1

I'm trying to create a piece of code that performs simple subtraction and prints a message based on the user's input, but I haven't been able to figure out how to get the user input to properly serve as the functions argument.

I keep getting the following error, even though I've included int along with the user input:

TypeError: '>=' not supported between instances of > 'NoneType' and 'int'

Do you have any clue what I'm missing?

The exact code I'm working on shown below:

def withdraw_money(current_balance, amount):
    if (current_balance >= amount):
        current_balance = current_balance - amount
        return current_balance

balance = withdraw_money(int(input("How much money do you have")), int(input("How much do your groceries cost")))

if (balance >= 0): 
    print("You're good")
else: 
    print("You're broke")

1 Answer 1

1

You are missing a return in else

so whenever you are broke your function is returning nothing and you are comparing nothing (None)

def withdraw_money(current_balance, amount):
    if (current_balance >= amount):
        current_balance = current_balance - amount
        return current_balance
    else: # need to return something if the above condition is False
        return current_balance - amount

balance = withdraw_money(int(input("How much money do you have")), int(input("How much do your groceries cost")))

if (balance >= 0): 
    print("You're good")
else: 
    print("You're broke")

PS: if you are checking whether the person is broke or not outside the function why are you checking the difference inside the function? (Your fnc can just return the difference and then outside you can check if it's -ive or +ive)

Sign up to request clarification or add additional context in comments.

3 Comments

That fixed it. Thanks, Kuldeep for providing the answer and explaining the reasoning behind also!
To answer your PS, I'm very new to programming, so I am just testing out how to use the return command.
PS, I will definitely mark it as the answer in just a moment. For whatever reason, StackOverflow is making me wait 10 minutes before I can mark it as the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.