함수정의 : def 함수이름():
실제 수행 내용 작성
def open_account():
print("새로운 계좌가 생성되었습니다.")
open_account()
def deposit(balance, money): # 입금 함수(잔액, 입금 금액)
print("입금이 완료되었습니다. 잔액은 {0}원입니다.".format(balance+money))
return balance+money
def withdraw(balance, money):
if balance >= money: # 잔액이 출금보다 많으면
print("출금이 완료되었습니다. 잔액은 {0}원입니다.".format(balance - money))
return balance - money
else:
print("출금이 완료되지 않았습니다. 잔액은{0}원입니다.".format(balance))
return balance
def withdraw_night(balance, money): # 저녁에 출금
commission = 100 # 수수료 100원
return commission, balance - money - commission # 튜플 형식으로 반환
balance = 0
balance = deposit(balance, 2000)
balance = withdraw(balance, 1000)
commission, balance = withdraw_night(balance, 500)
print("수수료는 {0}원이며, 잔액은{1}원입니다.".format(commission, balance))
def profile(name, age, main_lang):
print("이름: {0}\t나이: {1}\t주 사용 언어: {2}"
.format(name, age, main_lang))
profile('이나현', 28, "javascript")
같은 학교 같은 학년 같은 반 같은 수업 (기본값을 만듦: 값을 전달받지 않았을 때는 기본값 출력)
def profile(name, age=17, main_lang="python"):
print("이름: {0}\t나이: {1}\t주 사용 언어: {2}"
.format(name, age, main_lang))
profile('이나현')
profile('이익선')
키워드값을 이용한 함수 호출
def profile(name, age, main_lang):
print(name, age, main_lang)
profile(name="이나현", main_lang="python", age=25)
가변인자(다른 값을 반환할 때 매개변수를 사용해서 쓸 수 있음 )
def profile(name, age, lang1, lang2, lang3, lang4, lang5):
print("이름: {0}\t나이: {1}\t".format(name, age), end=" ") # end는 끝을 한 칸 띄어씀
print(lang1, lang2, lang3, lang4, lang5)
def profile(name, age, *language):
print("이름: {0}\t나이: {1}\t".format(name, age), end=" ") # end는 끝을 한 칸 띄어씀
for lang in language:
print(lang, end="")
print()
profile("이나현", 28, "python", "java", "c", "c++", "c#", "javascript")
profile("이익선", 25, "kotlin", "swift")
gun = 10
def checkpoint(soldiers): # 경계근무 나가는 군인의 수가 전달값
global gun # 전역 공간에 있는 gun 사용
gun = gun - soldiers # 지역변수: 할당이 안되면 쓸 수 없음 > 전역변수 사용
print("[함수 내] 남은 총: {0}".format(gun))
def checkpoint_ret(gun, soldiers):
gun = gun - soldiers
print("[함수 내] 남은 총: {0}".format(gun))
return gun
print(" 전체 총: {0}".format(gun))
# checkpoint(3) # 3auddl 경계근무 나감
gun = checkpoint_ret(gun, 2)
print("남은 총: {0}".format(gun))
def std_weight(height, gender): # 키는 m 단위(실수), 성별 "남자"/"여자"
if gender == "여자":
weight = 0.01*height * 0.01*height * 21
else:
weight = 0.01*height * 0.01*height * 22
weight = round(weight, 2)
print("키 {0}cm {1}의 표준 체중은 {2}kg입니다.".format(height, gender, weight))
std_weight(170, "남자")
std_weight(150, "여자")
def std_weight(height, gender):
if gender == "남자":
return height * height * 22
else:
return height * height * 21
height = 170 # cm 단위
gender = "남자"
weight = round(std_weight(height/100, gender), 2)
print("키 {0}cm {1}의 표준 체중은 {2}kg 입니다.".format(height, gender, weight))ㅏ무
정답과 사뭇 다른 내가 만든 답안.
다른점은 정답에서는 height와 gender를 변수에 지정해야하지만, 나는 함수의 인자로 print할 수 있게 만들었다.
부족한 점은 m 단위를 cm로 바꾸는데 round를 사용하는데 해당 부분을 몰라서 수정했다.
그래도 if문과 함수를 섞어서 썼다는 것에 칭찬 한가득 주는 중이다. ㅎㅎ