gpt4 book ai didi

python - 如何从 python 函数的列表中选择一个项目?

转载 作者:太空宇宙 更新时间:2023-11-03 20:24:23 25 4
gpt4 key购买 nike

我正在尝试使用带有列表和集合的函数以及 ifelif 语句创建一个菜单项。我还尝试在自己的单独模块中创建这些函数,然后将它们导入主模块中。

在下面的代码中,我创建了该函数,现在我想从链接到 inputifelif 语句开始陈述。

当我运行代码并选择第一个选项时,CMD 会弹出并显示“按任意按钮继续”,但不显示所需的输入。

我做错了什么?

def Main_Menu():
MainMenuItems = ['1. Bacon & Eggs-$2.00', '2. Full-Course Meal-$3.99', '3. Oatmeal-$1.00', '4. Hamburger & Fries-$3.00']
print(*MainMenuItems, sep='\n')

choice = int(input("Please select a menu item from the list above: "))

if choice is [0]:
print("You chose ", MainMenuItems[1])

最佳答案

您不需要 if 来执行此操作。而且你要知道列表索引是从0开始的,所以当你选择数字1时,你指的是0索引项:

只需打印即可完成:

print("You chose ", MainMenuItems[choice-1])

关于python - 如何从 python 函数的列表中选择一个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57943760/

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