-1

This is not working, Cant figure it out... i want it to print either error sentence or break.. I wanted to do it in a try/except, but that was not so good. And I'm new to python :-)

    while True:  
        unitFrom = input("Enter unit of temperature, either Fahrenheit, Kelvin or Celsius:") 
        list = ["Fahrenheit" , "Celsius" , "Kelvin"]
        if unitFrom.lower() in list:
            break
        else:
            print ("Wrong unit, try again")
            break
dejanualex
  • 3,872
  • 6
  • 22
  • 37
Lark
  • 1
  • 2
    never use the keyword `list` to define your lists – DirtyBit Jan 12 '21 at 10:45
  • 1
    Never use inbuilt declarations to define new variables. You should change 'list' to something else, since `list` is itself an inbuilt function. – shiv_90 Jan 12 '21 at 10:45

5 Answers5

1
  1. Never use the built-in keywords to define new variables.
  2. Take the list outside the loop to avoid initializing it on each iteration.
  3. You need to have the list in lowercase since you're checking the lower-cased input in the list:

Hence:

x_units = ["fahrenheit" , "celsius" , "kelvin"]
# or x_units = [x.lower() for x in x_units] if you do not wish to change the original list
while True:  
    unitFrom = input("Enter unit of temperature, either Fahrenheit, Kelvin or Celsius:") 
    if unitFrom.lower() in x_units:
        break
    else:
        print ("Wrong unit, try again")
        break
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
0

Do you want to print break or want to execute break ? and list = ["Fahrenheit" , "Celsius" , "Kelvin"] is created new everytime execute it before while True: and use something other than list as array name as list is a keyword

  answer_list = ["Fahrenheit" , "Celsius" , "Kelvin"]
while True:  
    unitFrom = input("Enter unit of temperature, either Fahrenheit, Kelvin or Celsius:") 
  
    if unitFrom.lower() in answer_list:
        break
    else:
        print ("Wrong unit, try again")
        break
0

The problem is that you're not using lowercase units. And then checking if "Kelvin" =="kelvin", which is never True.

Replace your list of units with lowercase or just use this code:

while True:  
    unitFrom = input("Enter unit of temperature, either Fahrenheit, Kelvin or Celsius:") 
    myList = ["Fahrenheit" , "Celsius" , "Kelvin"]
    myList = [unit.lower() for unit in myList] #transform all strings inside list to lowercase
    
    if unitFrom.lower() in myList:
        break
    else:
        print ("Wrong unit, try again")
        break

Also never use list as a variable name.

Jakub Szlaur
  • 1,852
  • 10
  • 39
0

As pointed by @dirtybit, you should take care of those points.

Set Access is faster than list, if list is of big size, you can try set to compare with input.

Solution :

units_set = set("fahrenheit" , "felsius" , "kelvin") # initializing it only once, the lower case values.
while True:  
    unit_from = input("Enter unit of temperature, either Fahrenheit, Kelvin or Celsius:") 
    if unit_from.lower() in units_set:
        # do something here.
        break
    else:
        print ("Wrong unit, try again")
Neeraj Sonaniya
  • 375
  • 4
  • 13
0

You can try this,

loopBool = True
while loopBool:
    unitList = ["fahrenheit", "celsius", "kelvin"]
    try:
            unitFrom = raw_input("Enter unit of temperature, either Fahrenheit, Kelvin or Celsius:")
            if unitFrom in unitList:
                inBool = True
            if  inBool == True:
               print("Success")
               loopBool = False
            else:
                raise Exception
    except:
            print ("Wrong unit, try again")

The problem might be input() function you use.

I do not want to change your code to much (You should think about lower in my case)

while True:
    unitFrom = raw_input("Enter unit of temperature, either Fahrenheit, Kelvin or Celsius:")
    unitList = ["Fahrenheit", "Celsius", "Kelvin"]
    if unitFrom in unitList:
        break
    else:
        print ("Wrong unit, try again")
        break

You can read further about it : NameError from Python input() function

Nott
  • 303
  • 1
  • 8