Python问题if/eli

2024-04-23 16:02:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使这个代码工作,但我似乎找不到正确的解决办法

while True:
    tname = input("Please enter the unit of the temperature: ")
    list=["Celsius","Kelvin","Fahrenheit"]
    if tname == list[0] or list[1] or list[2]:
        break
    elif tname is not list[0] or list[1] or list[2]:
        print("Not a valid unit. Please try again.")

我想程序停止时,无论是摄氏度,开尔文或华氏度输入,但程序停止,无论我写什么。你们知道怎么修吗?提前谢谢


Tags: orofthe代码程序trueinputunit
1条回答
网友
1楼 · 发布于 2024-04-23 16:02:06

技术上正确的答案是链式理解不是这样的;你必须这么做

if tname == list[0] or tname == list[1] or tname == list[2]:

但是你考虑过使用in

if tname in list:

或者类似地:

if tname not in list:

另外,我建议不要使用list作为列表的名称,因为这也是类型的名称

相关问题 更多 >