It's a recap of beginner level, from day 1 to day 14.
Style of 'if statement execution part':
it's hard to read things in a long sentence.
rather make it neat and simple with multiple indented blocks
#-----------------My_code -------------
if ask_menu == "report":
resource_left = print(f"Water : {resources['water']}ml \nMilk: {resources['milk']}ml \nCoffee: {resources['coffee']}g \nMoney: ${profit}")
coffee_serving()
return resource_left
#-----------------Angela's -------------
elif choice == "report":
print(f"Water: {resources['water']}ml")
print(f"Milk: {resources['milk']}ml")
print(f"Coffee: {resources['coffee']}g")
print(f"Money: ${profit}")
Consider scalability. Make it dynamic rather than hard coded. As drink menu can be modified.
#-----------------My_code -------------
while continue_serving:
elif ask_menu == 'latte' or ask_menu == 'cappuccino' or ask_menu == 'espresso':
execution
#-----------------Angela's -------------
while is_on:
else:
drink = MENU[choice]
I tried to make a list of ingredients to loop through each items, however, better to use existing dictionary.
In angela's code, parameter order_ingredients is a MENU[drink]'s dictionary which access to each ingredients. Items are milk, coffee, milk.
This methods is much more scalable, even though ingredients are modified it still can process regardless. My initial code was hard coded.
#------------My_code --------------------
def resource_check(drink, ingredient_type):
ingredients_check = MENU[drink]['ingredients'][ingredient_type]
resources_check = resources[ingredient_type]
if ingredients_check <= resources_check:
return True
elif ingredients_check > resources_check:
return False
def check(water_check, milk_check, coffee_check):
if water_check:
if milk_check:
if coffee_check:
print('Please insert coins.')
return True
else:
print(f"Sorry there is not enough coffee.")
else:
print(f"Sorry there is not enough milk.")
else:
print(f"Sorry there is not enough water.")
coffee_serving()
elif ask_menu == 'latte' or ask_menu == 'cappuccino' or ask_menu == 'espresso': #resource checked #espresso doesn't have milk in dict, can I add it or should I make additional if statement
water_check = resource_check(ask_menu, 'water')
coffee_check = resource_check(ask_menu, 'coffee')
milk_check = resource_check(ask_menu, 'milk')
if check(water_check, milk_check, coffee_check):
#-----------------Angela's --------------------------
def is_resource_sufficient(order_ingredients):
"""Returns True when order can be made, False if ingredients are insufficient."""
for item in order_ingredients:
if order_ingredients[item] > resources[item]:
print(f"Sorry there is not enough {item}.")
return False
return True
drink = MENU[choice] #choice is user input
else:
drink = MENU[choice]
if is_resource_sufficient(drink["ingredients"]): #access to drink's dict
process_coins()
Angela processed two things in one function, when I did it seperately.
#------------My_code --------------------
def money(name_of_coins):
ask_money = int(input(f"how many {name_of_coins}?: "))
return ask_money
quarters = money("quarters")
dimes = money("dimes")
nickles = money("nickles")
pennies = money("pennies")
user_money = round(0.25 * quarters + 0.1 * dimes + 0.05 * nickles + 0.01 * pennies, 2)
change = round((user_money - MENU[ask_menu]["cost"]), 2)
#-----------------Angela's --------------------------
def process_coins():
"""Returns the total calculated from coins inserted."""
print("Please insert coins.")
total = int(input("how many quarters?: ")) * 0.25
total += int(input("how many dimes?: ")) * 0.1
total += int(input("how many nickles?: ")) * 0.05
total += int(input("how many pennies?: ")) * 0.01
return total
parameter order_ingredients = MENU[choice]['ingredients']
-> key: milk, coffee, water inside dict. Each key assigned to item in for loop.
#-----------------Angela's --------------------------
#1
def is_resource_sufficient(order_ingredients):
"""Returns True when order can be made, False if ingredients are insufficient."""
for item in order_ingredients:
if order_ingredients[item] > resources[item]:
print(f"Sorry there is not enough {item}.")
return False
return True
drink = MENU[choice]
if is_resource_sufficient(drink["ingredients"]): #argument: MENU dictionary
#2
def make_coffee(drink_name, order_ingredients):
"""Deduct the required ingredients from the resources."""
for item in order_ingredients:
resources[item] -= order_ingredients[item]
print(f"Here is your {drink_name} ☕️. Enjoy!")
make_coffee(choice, drink["ingredients"])
Secret is... I had to modify the item in a dictionary, which is milk in espresso, because of my hard coded lines.
However, It worked fine except for that. I am proud of myself completing it. My first, nearly 100 lines of code on my own.
For the simplicity sake, I need to use for loops in this code.
Angela's total lines of code was 90, mine was 110.
Using loop can reduce the amount of code.
- Let's add docstrings under each functions.
MENU = {
"espresso": {
"ingredients": {
"water": 50,
"coffee": 18,
"milk": 0,
},
"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,
}
# TODO: 1. User input asking the menu
# TODO: 2. Turning off for maintenance
# TODO: 3. Check resources using report
# TODO: 4. Check enough resources to make coffee
def resource_check(drink, ingredient_type):
ingredients_check = MENU[drink]['ingredients'][ingredient_type]
resources_check = resources[ingredient_type]
if ingredients_check <= resources_check:
return True
elif ingredients_check > resources_check:
return False
def check(water_check, milk_check, coffee_check):
if water_check:
if milk_check:
if coffee_check:
print('Please insert coins.')
return True
else:
print(f"Sorry there is not enough coffee.")
else:
print(f"Sorry there is not enough milk.")
else:
print(f"Sorry there is not enough water.")
coffee_serving()
# TODO: 5. Input coins by user
def money(name_of_coins):
ask_money = int(input(f"how many {name_of_coins}?: "))
return ask_money
#
# TODO: 6. Calculate transaction
def calculate_transaction(money_insufficient, give_change, same, change):
if money_insufficient:
print("Sorry that's not enough money. Money refunded.")
continue_serving = False
elif give_change:
print(f"Here is ${change} dollars in change.")
return True
elif same:
return True
# TODO: 7. Add profit before making coffee
# TODO: 8-1. Make coffee deduct ingredients
def ingredients_deduction(drink, ingredient_type):
resources[ingredient_type] -= MENU[drink]['ingredients'][ingredient_type]
#TODO: 8-2. Print serving
#TODO: 10. serve the next customer by repeating from todo1
def coffee_serving():
continue_serving = True
profit = 0
while continue_serving:
ask_menu = input("What would you like? (espresso/latte/cappuccino): ").lower()
if ask_menu == "report":
print(f"Water : {resources['water']}ml \nMilk: {resources['milk']}ml \nCoffee: {resources['coffee']}g \nMoney: ${profit}")
elif ask_menu == 'off':
continue_serving == False
elif ask_menu == 'latte' or ask_menu == 'cappuccino' or ask_menu == 'espresso': #resource checked #espresso doesn't have milk in dict, can I add it or should I make additional if statement
water_check = resource_check(ask_menu, 'water')
coffee_check = resource_check(ask_menu, 'coffee')
milk_check = resource_check(ask_menu, 'milk')
if check(water_check, milk_check, coffee_check):
quarters = money("quarters")
dimes = money("dimes")
nickles = money("nickles")
pennies = money("pennies")
user_money = round(0.25 * quarters + 0.1 * dimes + 0.05 * nickles + 0.01 * pennies, 2)
print(user_money)
change = round((user_money - MENU[ask_menu]["cost"]), 2)
money_insufficient = (user_money < MENU[ask_menu]["cost"]) #bool True
give_change = (user_money > MENU[ask_menu]["cost"])
same = (user_money == MENU[ask_menu]["cost"])
if calculate_transaction(money_insufficient, give_change, same, change): #enoug or same
profit += MENU[ask_menu]["cost"]
print(profit)
ingredients_deduction(ask_menu, 'milk')
ingredients_deduction(ask_menu, 'water')
ingredients_deduction(ask_menu, 'coffee')
print(resources)
print(f'Here is your {ask_menu} ☕ Enjoy!')
coffee_serving()
-overwrite mode
-multi cursor : window: shift+alt / mac: option+shift
Mushroom Coffee is not just a drink, but a lifestyle upgrade. For those seeking a Mushroom Coffee Alternative, it’s packed with mushrooms that help enhance brain function and provide a sustained energy boost. It’s also incredibly gentle on the digestive system compared to regular coffee.