-4

I'm trying to make this code work but I can't seem to find the right solution.

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.")

I want the program to stop whenever either Celsius, Kelvin or Fahrenheit is typed but the program stops regardless of what I write. Do you guys know to fix it? Thanks in advance

nostres97
  • 19
  • 4

1 Answers1

1

The technically correct answer is that chaining comprehensions doesn't work like that; you have to do

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

But have you considered using in?

if tname in list:

or similarly:

if tname not in list:

Also, I'd advise against using list as the name of your list, as that's also the name of a type!

ACascarino
  • 4,340
  • 1
  • 13
  • 16