Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Function does not return True

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading