Iterating over dictionary and list items in a nestled for loop

Question:

How do I make it so the for loop statement is actually iterating (in order) over all the items in the list? (My best attempt below)

e.g
• Ham & Cheese (Hc) $5.00
Marinara tomato sauce

• Cheesy Garlic (Cg) $5.00
Served with garlicky aioli

menu = {"Pizzas" : {("Hc", "Ham & Cheese") : 5, ("Cg", "Cheesy Garlic") : 5, ("H", "Hawaiian") : 5, ("Cc", "Classic Cheese") : 5, ("Cv", "Classic Veggie") : 5, 
        ("M", "Margherita") : 8.50,("Hs", "Hot & Spicy Veggie") : 8.50, ("P", "Pepperoni") : 8.50, ("Ml", "Meat Lovers") : 8.50, 
        ("Bc","Buffalo Chicken") : 8.50, ("Cs", "Chicken Supreme") : 8.50,("Bb", "Beyond Beef") : 8.50},
        "Sides" : {("Cf", "Crinkle-Cut Fries") : 5.50,("Gb","Garlic Bread") : 4.49, ("Ct", "Chicken Tenders") : 8.99}, 
        "Drinks" : {("Co", "Coca Cola (1.5L)") : 4.79, ("S", "Sprite (1.5L)") : 4.79,("P", "Pump (750ml)") : 3.50, ("Ko", "Keri Orange Juice (350ml)") : 2.99}}

food_desc = ["Marinara tomato sauce", "Served with garlicky aioli", "Loaded with mozzarella cheese", "Stretchy, gooey sauce.",
             "Juicy pineapple", "Freshly chopped tomatoes", "Our explosive combination of jalapenos", "Meltingly delicious mozzarella", 
             "With a variety of meats", "Loaded with our ", "Over juicy chicken breast", "Covered with loads", 
             "Perfectly seasoned", "Fresh from the oven", "Flavorful chicken wings"]

def show_menu():
    print("Menu:n".center(60))
    print("Pizzas n")
    for (code, food), price in menu["Pizzas"].items():
            print(f"   • {food :<30} {'('+ code + ')': <10} ${price:.2f}")
            for description in food_desc:
                continue
            print(f"{description} n")

show_menu()

(Sorry, my code is really messy)

Asked By: GruffCheetah628

||

Answers:

This should give you enough to work with, check out the zip function.

for ((code, food), price), desc in zip(menu['Pizzas'].items(), food_desc):
    print(code, food, price, desc)

Output:

Hc Ham & Cheese 5 Marinara tomato sauce
Cg Cheesy Garlic 5 Served with garlicky aioli
H Hawaiian 5 Loaded with mozzarella cheese
Cc Classic Cheese 5 Stretchy, gooey sauce.
Cv Classic Veggie 5 Juicy pineapple
M Margherita 8.5 Freshly chopped tomatoes
Hs Hot & Spicy Veggie 8.5 Our explosive combination of jalapenos
P Pepperoni 8.5 Meltingly delicious mozzarella
Ml Meat Lovers 8.5 With a variety of meats
Bc Buffalo Chicken 8.5 Loaded with our 
Cs Chicken Supreme 8.5 Over juicy chicken breast
Bb Beyond Beef 8.5 Covered with loads

As a suggestion, I might consider restructuring your data like so:

food_iter = iter(food_desc)
menu = {category:{code:{'name':food, 
                        'price':value, 
                        'desc':next(food_iter) if category == 'Pizzas' else None
                        } 
                  for (code, food), value in info.items()} 
        for category, info in menu.items()}

Output:

{'Drinks': {'Co': {'desc': None, 'name': 'Coca Cola (1.5L)', 'price': 4.79},
            'Ko': {'desc': None,
                   'name': 'Keri Orange Juice (350ml)',
                   'price': 2.99},
            'P': {'desc': None, 'name': 'Pump (750ml)', 'price': 3.5},
            'S': {'desc': None, 'name': 'Sprite (1.5L)', 'price': 4.79}},
 'Pizzas': {'Bb': {'desc': 'Covered with loads',
                   'name': 'Beyond Beef',
                   'price': 8.5},
            'Bc': {'desc': 'Loaded with our ',
                   'name': 'Buffalo Chicken',
                   'price': 8.5},
            'Cc': {'desc': 'Stretchy, gooey sauce.',
                   'name': 'Classic Cheese',
                   'price': 5},
            'Cg': {'desc': 'Served with garlicky aioli',
                   'name': 'Cheesy Garlic',
                   'price': 5},
            'Cs': {'desc': 'Over juicy chicken breast',
                   'name': 'Chicken Supreme',
                   'price': 8.5},
            'Cv': {'desc': 'Juicy pineapple',
                   'name': 'Classic Veggie',
                   'price': 5},
            'H': {'desc': 'Loaded with mozzarella cheese',
                  'name': 'Hawaiian',
                  'price': 5},
            'Hc': {'desc': 'Marinara tomato sauce',
                   'name': 'Ham & Cheese',
                   'price': 5},
            'Hs': {'desc': 'Our explosive combination of jalapenos',
                   'name': 'Hot & Spicy Veggie',
                   'price': 8.5},
            'M': {'desc': 'Freshly chopped tomatoes',
                  'name': 'Margherita',
                  'price': 8.5},
            'Ml': {'desc': 'With a variety of meats',
                   'name': 'Meat Lovers',
                   'price': 8.5},
            'P': {'desc': 'Meltingly delicious mozzarella',
                  'name': 'Pepperoni',
                  'price': 8.5}},
 'Sides': {'Cf': {'desc': None, 'name': 'Crinkle-Cut Fries', 'price': 5.5},
           'Ct': {'desc': None, 'name': 'Chicken Tenders', 'price': 8.99},
           'Gb': {'desc': None, 'name': 'Garlic Bread', 'price': 4.49}}}
Answered By: BeRT2me
def main():
    while True:
        show_menu()
        choice = input("Please choose an item from the menu: ")
        if choice in menu["Pizzas"]:
            print(f"You have chosen {choice}")
            break
        else:
            print("That is not a valid option. Please try again.")

    for (code, food), price in menu["Pizzas"].items():
        if choice == code:
            print(f"You have chosen {food}")

    for (code, food), price in menu["Pizzas"].items():
        if choice == food:
            print(f"That will be ${price}")

    for (code, food), price in menu["Pizzas"].items():
        if choice == code or choice == food:
            quantity = int(input("How many would you like? "))

    for (code, food), price in menu["Pizzas"].items():
        if choice == code or choice == food:
            total = quantity * price

    for (code, food), price in menu["Pizzas"].items():
        if choice == code or choice == food:
            print(f"Your total is ${total}")

    payment = input("Would you like to pay now? ")

    if payment.lower() == "yes":  # this is how you make it so the user can enter yes/Yes/YES/yEs/yeS/Y and it will still work. 
                                   # You can do this with .lower() or .upper() on strings. 

        while True:  # this is an infinite loop. It will keep repeating until we tell it to stop with a break statement. 

            try:  # this try/except block will keep repeating until the user enters a valid number. 
            cash_tendered = float(input("How much cash would you like to tender? $"))  # we use float here in
Answered By: Tori Elstrom
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.