Can't stop if statement from executing even though False + Messy code? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Can't stop if statement from executing even though False + Messy code? (/thread-38468.html) |
Can't stop if statement from executing even though False + Messy code? - monkeydesu - Oct-17-2022 I'm doing the 100 days of Python on Udemy and am doing the day 15 challenge. I'm tasked with making a coffee machine, ask which drink from user, ask for money, compare total with price, then check reserves in machine, then reduce the reserves with requested coffee ingredients... Even if the reserves are not sufficient for the requested coffee, the program goes ahead with asking for money instead of looping back to the start of the program... ALSO I don't fully understand namespace, global scope, local scope. So I was wondering if that's the reason this is not working? (I have 8 weak warnings on the IDE) And can you maybe give me tips to clean up the namespace mess I have on my hands. Please and thank you! The output/bug: ------------------------------------------------------------------------------------------ Which would you like? A) espresso, B) latte, or C) cappuccino? c There isn't enough water. Please refill. There isn't enough milk. Please refill. How many quarters? Process finished with exit code -1 ------------------------------------------------------------------------------------------ Coffee_data: MENU = { "espresso": { "ingredients": { "water": 50, "milk": 0, "coffee": 18, }, "cost": 1.5, }, "latte": { "ingredients": { "water": 200, "milk": 150, "coffee": 24, }, "cost": 2.5, }, "cappuccino": { "ingredients": { "water": 250, "milk": 100, "coffee": 24, }, "cost": 3.0, } } resources = { "water": 300, "milk": 200, "coffee": 100, } US_MONEY = { "penny": 0.01, "nickel": 0.05, "dime": 0.10, "quarter": 0.25, }Coffee_machine: import Coffee_data water_reserve = Coffee_data.resources["water"] milk_reserve = Coffee_data.resources["milk"] coffee_reserve = Coffee_data.resources["coffee"] money_reserve = 0.0 sufficient_funds = False sufficient_reserves = True chosen_drink = "" def chosen_order(order): global sufficient_reserves global chosen_drink chosen_drink = "" if order == "report": print(f"Water: {water_reserve}ml") print(f"Milk: {milk_reserve}ml") print(f"Coffee: {coffee_reserve}g") print(f"Money: ${money_reserve}") elif order == "a": chosen_drink = "espresso" elif order == "b": chosen_drink = "latte" elif order == "c": chosen_drink = "cappuccino" return chosen_drink def transaction(chosen_drink): global sufficient_funds global change global money_reserve q_amount = float(input("How many quarters? ")) d_amount = float(input("How many dimes? ")) n_amount = float(input("How many nickels? ")) p_amount = float(input("How many pennies? ")) total_amount_paid = float((q_amount * Coffee_data.US_MONEY["quarter"]) + (d_amount * Coffee_data.US_MONEY["dime"]) + (n_amount * Coffee_data.US_MONEY["nickel"]) + (p_amount * Coffee_data.US_MONEY["penny"])) print(f"${total_amount_paid}") if total_amount_paid < Coffee_data.MENU[chosen_drink].get('cost'): print("Insufficient funds.") sufficient_funds = False else: change = total_amount_paid - Coffee_data.MENU[chosen_drink].get('cost') money_reserve += Coffee_data.MENU[chosen_drink].get('cost') print(f"Here is your change: ${change}") sufficient_funds = True return sufficient_funds, change def reserve_check(water_reserve, milk_reserve, coffee_reserve): global sufficient_reserves if water_reserve - chosen_order_water <= 0: print("There isn't enough water. Please refill.") if milk_reserve - chosen_order_milk <= 0: print("There isn't enough milk. Please refill.") if coffee_reserve - chosen_order_coffee <= 0: print("There isn't enough coffee. Please refill.") else: sufficient_reserves = True # main loop-------------------------------------------------------------------------------- while True: order = input("Which would you like? A) espresso, B) latte, or C) cappuccino? ").lower() chosen_order(order) if order != "report": chosen_order_water = (Coffee_data.MENU[chosen_drink]["ingredients"].get('water')) chosen_order_milk = (Coffee_data.MENU[chosen_drink]["ingredients"].get('milk')) chosen_order_coffee = (Coffee_data.MENU[chosen_drink]["ingredients"].get('coffee')) reserve_check(water_reserve, milk_reserve, coffee_reserve) if sufficient_reserves: transaction(chosen_drink) if sufficient_funds: water_reserve -= chosen_order_water milk_reserve -= chosen_order_milk coffee_reserve -= chosen_order_coffee print("Please enjoy your drink!") sufficient_funds = True sufficient_reserves = False RE: Can't stop if statement from executing even though False + Messy code? - deanhystad - Oct-17-2022 The way you compute sufficient_reserves is wrong. Work through the logic for an order where you don't have enough coffee, but all the other reserves are sufficient. What is the value of sufficient_reserves. You should also rethink how you are checking the reserves. How would you modify your program to allow making smoothies? Now you have fruit used in some recipes and not in others. Can you make your reserve_check more generic so it only checks items that are in the recipe? This kind of code is very difficult to exapand. Imagine that you made enough money from the coffee shop to open a restaurant where you have hundreds of ingredients. What would this code look like: if water_reserve - chosen_order_water <= 0: print("There isn't enough water. Please refill.") if milk_reserve - chosen_order_milk <= 0: print("There isn't enough milk. Please refill.") if coffee_reserve - chosen_order_coffee <= 0: print("There isn't enough coffee. Please refill.") else: sufficient_reserves = TrueYou can replace these lines of code with a loop and one comparison. The same code will work for 3 ingredients or a thousand. |