繁体   English   中英

只允许用户输入某些单词

[英]Only allow user to input certain words

这不起作用,无法弄清楚...我希望它打印错误语句或中断..我想在尝试/例外中做到这一点,但这并不是那么好。 我是 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
  1. 切勿使用内置关键字来定义新变量。
  2. 将列表放在循环之外以避免在每次迭代时对其进行初始化。
  3. 您需要将列表设为小写,因为您正在检查列表中的小写输入:

因此:

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

您要打印 break 还是要执行break 并且list = ["Fahrenheit", "Celsius", "Kelvin"]每次在之前执行它时都会创建新的while True:并使用 list 以外的东西作为数组名称,因为list是关键字

  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

问题是您没有使用小写单位。 然后检查"Kelvin" =="kelvin"是否永远不是True

用小写字母替换您的单位列表或仅使用以下代码:

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

也永远不要使用list作为变量名。

正如@dirtybit 所指出的,您应该注意这些要点。

Set Access 比 list 快,如果 list 比较大,可以尝试set和 input 比较。

解决方案:

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

你可以试试这个

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

问题可能是您使用的 input() function。

我不想将您的代码更改太多(在我的情况下您应该考虑降低)

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

您可以进一步阅读: NameError from Python input() function

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM