[PYTHON]

[인프런]함수 기본개념

b___gly 2022. 2. 25. 16:40
def open_account():
    print("새로운 계좌가 생성되었습니다.")

def deposit(balance,money):
    print("입금이 완료되었습니다. 잔액은 {0}원입니다.".format(balance+money))
    return balance + money

def withdraw(balance,money):
    if balance >= money :
        print("출금이 완료되었습니다. 잔액은 {0}원입니다.".format(balance-money))
    else :
        print("출금이 완료되지 않았습니다. 잔액은 {0}원입니다.".format(balance))
        return balance - money

def withdraw_night(balance, money):
    commision = 200
    print("수수료는 {0}원이며, 잔액은 {1}원입니다.".format(commision, balance - money - commision))
    return commision, balance - money - commision

balance = 0
balance = deposit(balance,1000)
# balance = withdraw(balance,2000)
commision, balance = withdraw_night(balance, 200)

 

  • 기본값
def profile(name, age=17, main_lang="python"):
	print("이름 : {0} \t 나이 : {1} \t 주 사용 언어:{2}".format(name, age, main_lang))

 

  • 가변인자
def profile(*language):
	for lang in language:
    print(lang)

 

  • 지역변수와 전역변수 (전역변수 많이 쓰는 건 좋지x)
gun = 10
def checkpoint(soldiers):
	gun = 20
    gun = gun - soldiers
    print("[함수내]남은 총 : {0}".format(gun))
    
print("전체 총:{0}".format(gun))
checkpoint(2)
print("남은 총:{0}".format(gun))

//지역변수 gun = 20, 전역변수 gun = 10

//함수밖의 gun(전역변수)를 쓰려면 아래와 같이

gun = 10
def checkpoint(soldiers):
	global gun
    gun = gun - soldiers
    print("[함수내]남은 총 : {0}".format(gun))
    
print("전체 총:{0}".format(gun))
checkpoint(2)
print("남은 총:{0}".format(gun))