gpt4 book ai didi

python - 如何将输入数据链接到列表项

转载 作者:行者123 更新时间:2023-12-01 07:18:17 25 4
gpt4 key购买 nike

编写一个Python程序 - 场景是中餐馆,订单以整数输入,对应于菜单项和价格。无法弄清楚如何将输入的数字链接到相应的菜单项和价格。有两份 list ,一份是菜单,一份是价格。如果用户想要大米等,则输入 1。想要显示订单摘要,其中显示菜单项和价格并为整个订单创建总计。

查看了 Python 书籍,但找不到如何解决我的问题

menu = ['rice', 'egg fried rice', 'prawn crackers']
price = [1.00, 2.00, 1.50]
again = True
<小时/>
while again:


incorrect_menu_item = True

while incorrect_menu_item:
menu_item = int(input('Enter menu item: '))

if menu_item >=1 and menu_item <= len (menu):
incorrect_menu_item = False
print('menu item accepted')
new_order.append(menu_item)
new_order.sort()

else:
print('incorrect input')

another_item = input('Another item? n to stop, enter to continue: ')
if another_item == 'n':
again = False
print('Thank you, order is complete')

已尝试将输入数字链接到菜单和价目表的索引,但不起作用

最佳答案

实现此目的的一种方法是使用字典数组,即一组键值对。它可能看起来有点像这样:

menu = [
{
"index": 59,
"name": "Plain rice",
"price": 1.2
},
{
"index": 33,
"name": "Something else",
"price": 2.5
}
]

然后您可以像这样查询:

for i, item in enumerate(menu):
if item["index"] == menu_item:
# Do something with the correct item
# e.g: item["price"] to get the price

关于python - 如何将输入数据链接到列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57841837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com