ImCanaan
ImCanaan

Reputation: 35

Populating a function argument with user input in Python

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")

Upvotes: 1

Views: 109

Answers (1)

Kuldeep Singh Sidhu
Kuldeep Singh Sidhu

Reputation: 3856

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)

Upvotes: 1

Related Questions