Python in 30 Days - Day Two
Day two was all about dictionaries, which have always been a real struggle for me. I’ve been told by many people that they’ve also struggled with them in the past, so at least I know I’m not alone.
The Challenge:
You’re building a simple restaurant order sytem.
Write code that:
- Creates a dictionary of menu items with their prices (at least 5 items)
- Creates an empty list called order
- Writes a function
add_item(order, item_name, menu)that:
- Adds the item to the order list if it exists in the menu
- Prints a warning if the item isn’t on the menu
- Writes a function
get_total(order, menu)that returns the total cost of everything in the order - Adds at least 3 items to the order (try adding one that doesn’t exist)
- Prints the final order list and the total, formatted cleanly
My Code:
menu = {
'Burger': 15,
'Fries': 5,
'Milkshake': 9,
'Salad': 6,
'Brownie': 3
}
order = []
total = 0
def add_item(order, item_name, menu):
if item_name in menu:
order.append(item_name)
print(f"Added {item_name} to order.")
else:
print(f"Sorry, {item_name} is not on the menu!")
def get_total(order, menu):
total = 0
for i in order:
price = menu[i]
total += price
return total
add_item(order, 'Burger', menu)
add_item(order, 'Pizza', menu)
add_item(order, 'Fries', menu)
add_item(order, 'Milkshake', menu)
bill = get_total(order, menu)
print("\nYour order:\n")
print(*order, sep="\n")
print(f"\nTotal: ${bill}")
The results of running the code are:

