Oct-17-2022, 01:46 AM
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:
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