I’m not sure how to do it. I tried it below, but it didn’t work, as you might expect. I’m attempting to create an ordering software in which a user orders food using the same keys as in order_menu and it is added to order_dictionary. Then, using the keys on the list, I calculate and add the order price using the values from order menu.
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 = {"C", "Hot Dog", "Chicken Nugget", "S"}
for food, values in order_menu.items():
if food in order_dictionary:
print(values)
>Solution :
FYI in your code order_dictionary is not a dictionary, it is a set.
The problem you have currently is the keys in order_menu are tuples – a pair of values.
So you need something like:
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_keys = {"C", "Hot Dog", "Chicken Nugget", "S"}
for food, values in menu.items():
if food[0] in order_keys:
print(values)
Another way would be to ‘unpack’ the tuple already in your for loop like this:
for (food_key, food_name), values in menu.items():
if food_key in order_keys:
print(values)