-2
def manage_account(balance, choice, money):
    if choice == '+':
        balance = balance + money
        return balance
    elif choice =='-':
        if balance > money:
            balance= balance - money
            return balance
        elif balance < money:
            print('There is not enough money in your account')
    else:
        print('ERROR')

balance = 10000    
balance = manage_account(balance, '-', 20000)
balance = manage_account(balance, '-', 5000)
balance = manage_account(balance, '=', 20000)
balance = manage_account(balance, '+', 1000)

why nontype problem happened? Ans how can i escape this awful situation?

  • 4
    Your function lacks a `return` statement. Hence it returns None. – Jonathan Scholbach Sep 28 '21 at 11:20
  • 1
    Does [this](https://stackoverflow.com/a/67439738/10952503) help you ? (other another answer) – Elikill58 Sep 28 '21 at 11:22
  • 2
    A hint for you next question: Please always include the error message you get. In this case it was quite easy to spot your problem, but in future this helps others help you. – Jonathan Scholbach Sep 28 '21 at 11:26
  • 1
    And another hint: Try to reduce your code to a minimum example: https://stackoverflow.com/help/minimal-reproducible-example And you could use a debugging technique, such as using `breakpoint()`. – Jonathan Scholbach Sep 28 '21 at 11:27

1 Answers1

3

Your function lacks a return statement, for the case that the account amount gets negative. Then it returns None.

That is why balance = manage_account(balance, '-', 20000) is None, and when you call the function the second time, an error is raised at balance - money. An analogue problem occurs when passing '=' as parameter choice to the function a couple of lines below.

Depending on what is the desired behavior of your function, you should consider using a raise ValueError instead of printing Error.

Jonathan Scholbach
  • 4,925
  • 3
  • 23
  • 44