# Making value2 in a ((key1, key2) : (value1, value2)) dictionary iterable

How do I make this work? I understand that “total_price” isn’t iterable but I’m still unsure of how to fix it.

``````order_menu = {("C", "Chips (Scoops)") : 2.80, ("F", "Fish (Battered)") : 2.90, ("Fc", "Fish (Crumbed)") : 4.90, ("Fib", "Filet (Battered)") : 6.90, ("Fic", "Filet (Crumbed)") : 6.90,
("Hd", "Hot Dog") : 2.60, ("S", "Sausage") : 2.60, ("Mp" , "Meat Patty (Homemade)") : 3.90, ("Cr", "Crabstick") : 2.50, ("Sr", "Spring Roll (Homemade)") : 2.80,
("Cr", "Curry Roll (Homemade)"): 2.80, ("Pof", "Potato Fritter (Homemade)") : 1.20, ("Paf", "Paua Fritter (Homemade)") : 5.90, ("Cn", "Chicken Nugget") : 1,
("Mh", "Mini Hot Dog (On a stick)") : 1.20, ("Pf", "Pineapple Fritter") : 2.50}
order_dictionary = {"Chips (Scoops)" : 3, "Curry Roll (Homemade)" : 5, "Sausage" : 2}
ordered_food = {}

for food, quantity in order_dictionary.items():
for (key, food_name), price in order_menu.items():
if food == food_name:
total_price = quantity * price
print("• {} x{} (\${:.2f})".format(food, quantity, total_price))
ordered_food[(food, quantity)] = (price, total_price)
buy = input("Type '-1' to cancel, press any other key to continue.)")
active_ordering = False
else:
for (food, quantity), (price, total_price) in ordered_food.items():
print("• {}    Qty: {}    \${} each    Total \${:.2f}".format(food, quantity, price, total_price))
total = round(sum(total_price), 2)
print(total)
``````

Nowhere in this scenario do you seem to be using a dict as a dict (a way to quickly find one particular value based on a key). Instead, you’re looping over everything in all three of them. Order_menu would make sense as a dictionary, but only if you use the same key to order.

Otherwise, don’t use a dict in the first place. You could just make ordered_food a list or a set, containing tuples of (food, quantity, price, total_price). You’re sticking the values in and extracting them later. There doesn’t seem to be a reason to put some of them in the “key” slot and some in the “value” slot, since you’re not doing lookups.

``````ordered_food = []

# order loop
ordered_food.append((food, quantity, price, total_price))

# purchase loop
for food, quantity, price, total_price in ordered_food:
print(f"• {food}   Qty: {quantity}     \${price} each   Total \${total_price:.2f}")
``````

I think there are better ways to structure your data and the code, but this removes the complexity of the dict in a place where it’s not used.

This is the same topic (and by the same user) as the following:

@moderators, will you please consolidate them under the original post (#1 above)? It’s very difficult to follow the problem, advice, and solutions attempted.

1 Like