헬린코린이

[Python] 4일차 본문

Programming/Python

[Python] 4일차

HCD 2023. 4. 25. 20:55
# 함수 - Chapter
# 함수 정의는 def로 시작 함수이름 콜론으로 마무리
def open_account():
    print("새로운 계좌가 생성되었습니다.")

open_account()


#전달값과 반환값 - Chapter
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,1000)
print(balance)
balance = withdraw(balance,500)
commission,balance = withdraw_night(balance,500)
print("수수료는 {0}원이며, 잔액은 {1} 원입니다." .format(commission,balance))



#기본값 - Chapter
# 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="파이썬"):
    print("이름 : {0}\t나이 : {1}\t주 사용 언어: {2}"\
          .format(name,age,main_lang)) #줄바꿈해서 사용하려면 \이거 스면 된다 하나로 인식
    
profile("유재석")
profile("김태호")


#키워드 값  - Chapter
def profile(name, age, main_lang):
    print(name, age, main_lang)

profile(name="유재석",main_lang="파이썬",age=20)
profile(main_lang="자바", age=25, name="김태호")


#가변인자- Chapter
# 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("유재석",20,"Python","Java","C","C++","C#")
profile("김태호",25,"Kotlin","Swift")


#지역변수 전역변수- Chapter
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(2)
gun = checkpoint_ret(gun,2)
print("남은 총 : {0}".format(gun))

 

 

자료 : https://www.youtube.com/watch?v=kWiCuklohdY&t=1574s

Comments