如何对(key: value, value)型字典切片实现菜单分段打印?
解决菜单分段打印问题
问题根源
- 原循环错误使用
FOOD_DESC[0]/FOOD_DESC[1],会将单个描述字符串拆分为字符迭代,导致每个菜品对应一个字符,完全偏离预期。 - 第二个循环重新遍历整个
MENU,导致菜品从头输出,无法延续到后续分类。
修正方案
将MENU转为有序列表后切片,匹配对应范围的描述文本,确保分段打印时菜品索引、名称、价格延续之前的进度:
MENU = {1 : ("Ham & Cheese", 5), 2 : ("Cheesy Garlic", 5), 3 : ("Hawaiian", 5), 4 : ("Classic Cheese", 5), 5 : ("Classic Veggie", 5), 6 : ("Margherita", 8.50), 7 : ("Hot & Spicy Veggie", 8.50), 8 : ("Pepperoni", 8.50), 9 : ("Meat Lovers", 8.50), 10: ("Buffalo Chicken", 8.50), 11 : ("Chicken Supreme", 8.50), 12 : ("Beyond Beef", 8.50), 13 : ("Crinkle-Cut Fries", 5.50), 14 : ("Garlic Bread", 4.49), 15 : ("Chicken Tenders", 8.99), 16 : ("Coca Cola (1.5L)", 4.79), 17 : ("Sprite (1.5L)", 4.79), 18 : ("Pump (750ml)", 3.50) } FOOD_DESC = ["Marinara tomato sauce topped with a layer of tender ham and lashings of mozzarella cheese.", "Served with garlicky aioli, marinara tomato sauce, and lots of creamy mozzarella cheese.", "Loaded with mozzarella cheese, ham, and chunks of delicious pineapple on top. A true Kiwi classic.", "Stretchy, gooey, melting mozzarella, generously layered over traditional marinara tomato sauce.", "Juicy pineapple, green pepper, and red onion topped with mozzarella for a vibrant flavour explosion.", "Freshly chopped tomatoes, a drizzle of pesto, and mounds of bubbling mozzarella.", "Our explosive combination of jalapenos, red onion, green and red capsicum, and melted mozzarella cheese.", "Meltingly delicious mozzarella, traditional marinara tomato sauce, with a pepperoni kick. An authentic pizza.", "A variety of meats piled on top of classic marinara sauce and topped with BBQ sauce, satisfy all your meaty cravings.", "Iconic Buffalo sauce, a hot shot of jalapenos, pieces of Southern-style fried chicken, and a creamy ranch drizzle.", "Over juicy chicken breast, red onion, and the cheesy richness of mozzarella, our tangy BBQ sauce tastes wonderful.", "Covered with loads of melted mozzarella cheese, red onion, and well-seasoned meat.", "Perfectly seasoned, crisp-cut, and lightly browned.", "Fresh from the oven, buttery and garlicky, with a parsley garnish.", "Flavorful chicken wings. Marinated in spices and fried till golden.", "Refreshing cola with bubbly fizz.", "Crisp, lemon-lime flavored soda.", "Cool, fruity pump drink."] def show_menu(): print("*"*100 + "\n" + "Menu:\n".center(65)) # 转换为列表以便切片(Python3.7+字典默认有序,可按插入顺序拆分) menu_items = list(MENU.items()) pizza_count = 12 # 披萨类菜品数量 # 打印披萨分类 print("Pizzas") for (idx, (food, price)), desc in zip(menu_items[:pizza_count], FOOD_DESC[:pizza_count]): print(f"[{idx : <2}]: {food : <45} ${price:.2f}\n {desc}\n") # 打印小食与饮料分类 print("\nSides & Drinks") for (idx, (food, price)), desc in zip(menu_items[pizza_count:], FOOD_DESC[pizza_count:]): print(f"[{idx : <2}]: {food : <45} ${price:.2f}\n {desc}\n") show_menu()
关键修正点
- 将
MENU.items()转为列表,通过切片menu_items[:12]和menu_items[12:]分离披萨与后续分类,确保索引和菜品延续。 - 对应
FOOD_DESC也按相同切片范围匹配,保证每个菜品对应正确的描述文本。 - 补充了
FOOD_DESC中缺失的3个饮料描述,避免迭代长度不匹配报错。
内容的提问来源于stack exchange,提问作者GruffCheetah628
相关产品推荐
相关产品推荐

