gpt4 book ai didi

Python 2 枚举示例

转载 作者:太空宇宙 更新时间:2023-11-04 09:22:55 24 4
gpt4 key购买 nike

我发现这段代码应该说明枚举是如何工作的。我知道 Python 3 现在有所不同,但我想理解这个例子。我需要输入什么才能让 print("You choose the easy option") 执行?

到目前为止,我已经尝试了 1EasyChoice.Easy,但没有成功。

def enum(**enums):
return type('Enum', (), enums)

Choice = enum(Easy = 1, Medium = 2, Hard = 3)
choice = input("Enter choice: ")

if choice == Choice.Easy:
print("You chose the easy option")
elif choice == Choice.Medium:
print("You chose the medium option")
elif choice == Choice.Hard:
print("You chose the hard option")
else:
print("You should choose one of the three levels!")

最佳答案

用户输入的input是string类型,Choice里面是integer。做出改变:

Choice = enum(Easy = '1', Medium = '2', Hard = '3')

choice = int(input("Enter choice: "))

在这种情况下你需要处理异常值错误

关于Python 2 枚举示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59194240/

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