Python Basics: Type Error

Asked 2 years ago, Updated 2 years ago, 45 views


def open_account():
    print("A new account has been created.")

def deposit(balance, money):
    print("Deposit Completed" The balance is KRW {0}."format(balance + money))
    return balance + money

def withdraw(balance, money):
    if balance >= money:
        print("Withdrawal completed. The balance is KRW {0}."format(balance - money))
        return balance - money
    else:
        print("Withdrawal not completed". The balance is KRW {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("Fee: {0} won; balance is {1} won."format(commssion, balance))

I'm practicing the deposit and withdrawal program.

return commission, balance-money-commission and commission, balance = draw_night(balance, 500) There was a type error but I don't know the cause. I would appreciate it if you could tell me if this is a code problem or a program setting problem.

python error

2022-09-20 19:08

2 Answers

def withdraw(balance, money):
    if balance >= money:
        print("Withdrawal completed. The balance is KRW {0}."format(balance - money))
        return balance - money
    else:
        print("Withdrawal not completed". The balance is KRW {0}."format(balance))        

The return statement is missing from this function under else condition. If the Python function ends without return, it returns None. That is,

def withdraw(balance, money):
    if balance >= money:
        print("Withdrawal completed. The balance is KRW {0}."format(balance - money))
        return balance - money
    else:
        print("Withdrawal not completed". The balance is KRW {0}."format(balance))    
    return None    

It's like that.

So if you do the balance=withdraw(balance, 2000) statement in the actual run, balance will be None. Because None is not a numeric value, withdraw_night(None,500) is performed later, and an error occurred while trying to calculate None and integer values in the function.


2022-09-20 19:08

def draw(balance, money):
    if balance >= money:
        print("Withdrawal completed. The balance is KRW {0}."format(balance - money))
        return balance - money
    else:
        print("Withdrawal not completed". The balance is KRW {0}."format(balance))        
        You forgot this part, "return balance"

If the withdraw function else, you omitted the return.


2022-09-20 19:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.