Coding/Python

[python] ν•¨μˆ˜(ν•¨μˆ˜, 전달값과 λ°˜ν™˜κ°’, ν‚€μ›Œλ“œκ°’, κΈ°λ³Έκ°’, κ°€λ³€μΈμž, μ§€μ—­λ³€μˆ˜μ™€ μ „μ—­λ³€μˆ˜)

κΉ€μœ λ‹ˆμ½˜ 2021. 10. 21. 15:04

ν•¨μˆ˜

def deposit(balance, money):
    print("μž…κΈˆμ΄ μ™„λ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€. μž”μ•‘μ€ {0}μ›μž…λ‹ˆλ‹€".format(balance+money))
    return balance + money

 

전달값과 λ°˜ν™˜κ°’

def deposit(balance, money):
    print("μž…κΈˆμ΄ μ™„λ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€. μž”μ•‘μ€ {0}μ›μž…λ‹ˆλ‹€".format(balance+money))
    return balance + money

# ν•¨μˆ˜ λ‚΄μ—μ„œ μ–΄λ–€ λ™μž‘μ„ ν•˜κ³  λ‚˜μ„œ νŠΉμ • 'κ°’'을 λ°˜ν™˜ν•΄μ€„ λ•Œ return 을 μ‚¬μš©ν•©λ‹ˆλ‹€.
# 그러면 κ·Έ ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜λŠ” κ³³μ—μ„œ λ°˜ν™˜λ°›μ€ return 값을 λ³€μˆ˜μ— λ‹΄μ•„μ„œ 이후에 μ‚¬μš©μ΄ κ°€λŠ₯ν•©λ‹ˆλ‹€.


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): #저녁에 좜금
    commision = 100 #수수료 100원
    return commision, balance - money - commision

balance = 0 #μž”μ•‘
balance = deposit(balance, 1000)
balance = withdraw(balance, 600)
commision, balance= withdraw_night(balance, 200)
print("수수료 {0}원이며, μž”μ•‘μ€ {1}원 μž…λ‹ˆλ‹€.".format(commision,balance))

 

ν‚€μ›Œλ“œκ°’

def profile(name, age, main_lang):
    print("이름 : {0}\tλ‚˜μ΄ {1}\t μ£Ό μ‚¬μš© μ–Έμ–΄: {2}"\
          .format(name, age, main_lang))

profile("μœ μž¬μ„", 20, "파이썬")
profile("μœ μž¬μ„", 25, "μžλ°”")

#같은 학ꡐ 같은 반 같은 μˆ˜μ—…
def profile(name, age=17, main_lang="python"): #λ§Œμ•½ λ‚˜μ΄, μ–Έμ–΄ 전달받지 μ•Šμ•˜μ„ λ–„ 기본적으둜 17κ³Ό 파이썬 ν˜ΈμΆœλ°›μŒ
    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="파이썬",age=20)


κ°€λ³€μΈμž

μ•„λž˜μ˜ ν•¨μˆ˜λŠ” lang의 개수λ₯Ό 일일이 λ§žμΆ°μ„œ μž‘μ„±ν•΄μ€˜μ•Όν•˜λŠ” λ²ˆκ±°λ‘œμ›€μ΄ μžˆλ‹€. 

def profile(name, age, lang1, lang2, lang3, lang4, lang5):
    print("이름 : {0}\tλ‚˜μ΄ : {1}\t".format(name, age),end=" ") #end=" "λŠ” ν”„λ¦°νŠΈλ¬Έ λλ‚œ ν›„ μ€„λ°”κΏˆ ν•˜μ§€X
    print(lang1, lang2, lang3, lang4, lang5)

λ”°λΌμ„œ, κ°€λ³€μΈμžλ₯Ό μ‚¬μš©ν•œλ‹€! end=" "λŠ” printλ¬Έ λλ‚˜λ„ ν–‰ λ°”κΏˆ μ•ˆν•œλ‹€λŠ” 것

def profile(name, age, *language):
    for lang in language:
        print(lang,end=" ")
    print()


profile("μœ μž¬μ„",20, "python", "java", "C", "C++", "C#")
profile("κΉ€νƒœν˜Έ",20, "kotlin", "swift")


μ§€μ—­λ³€μˆ˜μ™€ μ „μ—­λ³€μˆ˜

gun = 10 #μ „μ—­λ³€μˆ˜
def checkpoint(soldiers): #경계근무
    global gun #전역곡간에 μžˆλŠ” gun μ‚¬μš©
    gun = gun - soldiers
    print("[ν•¨μˆ˜ λ‚΄] 남은 총 : {0}".format(gun))

checkpoint(2) #2λͺ…이 경계근무 λ‚˜κ°
print("남은 총 : {0}".format(gun))

μ „μ—­λ³€μˆ˜ μ‚¬μš©μ€ ꢌμž₯λ˜μ§€ μ•ŠλŠ”λ‹€. ν•¨μˆ˜κ°€ λ³΅μž‘ν•΄μ§€λ©΄ νž˜λ“€κΈ° λ•Œλ¬Έμ—! 

νŒŒλΌλ―Έν„°λ‘œ λ°›μ•„ μ‚¬μš©ν•˜λŠ” 것을 ꢌμž₯! λ”°λΌμ„œ μ•„λž˜μ™€ 같이 μ“°λ©΄ μ’‹μŒ!

#μ „μ—­λ³€μˆ˜ μ“°λŠ” 것 ꢌμž₯λ˜μ§€ μ•ŠμŒ, νŒŒλΌλ―Έν„°λ‘œ λ°›λŠ” 것 ꢌμž₯ 

def checkpoint_ret(gun, soldiers):
    gun = gun - soldiers
    print("[ν•¨μˆ˜ λ‚΄] 남은 총 : {0}".format(gun))
    return gun
gun = checkpoint_ret(gun, 1)
print("전체 총 : {0}".format(gun))

 

ν€΄μ¦ˆ 

ν‘œμ€€ 체쀑을 κ΅¬ν•˜λŠ” ν”„λ‘œκ·Έλž¨ μž‘μ„±ν•˜μ—¬λΌ 
*ν‘œμ€€ 체쀑: 각 개인의 킀에 μ λ‹Ήν•œ 체쀑
(성별에 λ”°λ₯Έ 곡식)
λ‚¨μž : ν‚€ * ν‚€ * 22
μ—¬μž : ν‚€ * ν‚€ * 21

쑰건1: ν‘œμ€€ 체쀑은 λ³„λ„μ˜ ν•¨μˆ˜ λ‚΄μ—μ„œ 계산
* ν•¨μˆ˜λͺ…: std_weight
* 전달값: ν‚€(height), 성별(gender)
쑰건2: ν‘œμ€€ 체쀑은 μ†Œμˆ˜μ  λ‘˜μ§Έμžλ¦¬κΉŒμ§€ ν‘œμ‹œ


(좜λ ₯ 예제)

ν‚€ 175cm λ‚¨μžμ˜ ν‘œμ€€ 체쀑은 67.38kgμž…λ‹ˆλ‹€.

 

λ‚΄κ°€ ν‘Ό 것 

 

더보기
def std_weight(height, gender):
    if gender == "λ‚¨μž":
        weight = round(height * height * 22,2)
    else:
        weight = round(height * height * 21,2)
    print("ν‚€ {0}cm {1}의 ν‘œμ€€ 체쀑은 {2}kgμž…λ‹ˆλ‹€.".format(height, gender, weight))

 

ν•΄μ„€

 

더보기
def std_weight(height, gender) : #ν‚€ m λ‹¨μœ„, 성별 "λ‚¨μž"/"μ—¬μž"
    if gender == "λ‚¨μž":
        return height * height * 22
    else:
        return height * height * 21

height = 174 #çm λ‹¨μœ„
gender = "λ‚¨μž"
weight = round(std_weight(height/100,gender),2)
print("ν‚€ {0}cm {1}의 ν‘œμ€€ 체쀑은 {2}kgμž…λ‹ˆλ‹€.".format(height, gender, weight))