파이썬 기초 : 타입에러
조회수 597회
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))
return balance - money
else:
print("출금이 완료되지 않았습니다. 잔액은 {0}원 입니다.".format(balance))
def withdraw_night(balance, money):
commssion = 100
return commssion, balance - money - commssion
balance = 0
balance = deposit(balance, 1000)
balance = withdraw(balance, 2000)
commssion, balance = withdraw_night(balance, 500)
print("수수료 : {0}원이며, 잔액은 {1}원 입니다.".format(commssion, balance))
입출금 프로그램 연습중 입니다.
return commssion, balance - money - commssion
부분과 commssion, balance = withdraw_night(balance, 500)
부분에서
타입 에러가 났는데 도저히 원인을 모르겠습니다.
이게 코드 문제인지 프로그램 설정 문제인지 알려주시면 감사하겠습니다.
2 답변
-
def withdraw(balance, money): if balance >= money: print("출금이 완료되었습니다. 잔액은 {0}원 입니다.".format(balance - money)) return balance - money else: print("출금이 완료되지 않았습니다. 잔액은 {0}원 입니다.".format(balance))
이 함수에서
else
조건에서return
문이 없습니다. 파이썬 함수에서return
이 없이 종료되면,None
을 반환하게 됩니다. 즉,def withdraw(balance, money): if balance >= money: print("출금이 완료되었습니다. 잔액은 {0}원 입니다.".format(balance - money)) return balance - money else: print("출금이 완료되지 않았습니다. 잔액은 {0}원 입니다.".format(balance)) return None
과 같습니다.
그래서 실제 실행에서
balance = withdraw(balance, 2000)
문을 수행하면,balance
는None
이 됩니다.None
은 수치값이 아니기 때문에, 이후에withdraw_night(None, 500)
이 수행되고, 함수 안에서-
연산에서None
과 정수값의 연산을 시도하다가 타입이 다르다는 에러가 발생한 것입니다. -
def withdraw(balance, money): if balance >= money: print("출금이 완료되었습니다. 잔액은 {0}원 입니다.".format(balance - money)) return balance - money else: print("출금이 완료되지 않았습니다. 잔액은 {0}원 입니다.".format(balance)) return balance #이 부분 빼먹으셨어용
withdraw 함수 else 일경우 리턴을 빼먹으셨어여
댓글 입력