파이썬 기초 : 타입에러

조회수 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 답변

  • 좋아요

    0

    싫어요
    채택 취소하기
    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) 문을 수행하면, balanceNone 이 됩니다. 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 일경우 리턴을 빼먹으셨어여

    • 아 맞네요 감사합니다. 승현 2021.1.8 11:48

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)