Python Function does not return True

Question:

pretty new to python and trying out some small exercices.
Im stuck at a funciton which does not return true:

def pay(ch):
    price = MENU[ch]["cost"]
    quarters = int(input("How many quarters?")) * 0.25
    dimes = int(input("How many dimes?")) * 0.10
    nickles = int(input("How many nickles?")) * 0.05
    pennies = int(input("How many pennies?")) * 0.01
    budget = quarters + dimes + nickles + pennies
    global money
    if budget >= price:
        money += price
        print(f"Your change: {round((budget - price),2)} ")
        return True
    else:
        print("Not enough money")
        return False

I printed it out, if budget is higher than price, balance does gett added to money, and the print also executes, but it does not return True.
Here im calling the function:

if enough(choice)== True:
    pay(choice)
    print(pay(choice))
    if pay(choice) == True:
        make_coffee(choice)
    else:
        print("Nope")

I really dont understand why it does not return True, but executes the print statement.
Same goes for else, print statement executes, but no return False

Asked By: Retr0

||

Answers:

You are paying (=calling pay function) more than once!

You should call the function once and save the result in a variable: then you can do whatever you want with it

result = pay(choice)
# print(result)
if result:
    make_coffee(choice)
else:
    print("Nope")
Answered By: Lorenzo Mogicato
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.