簡體   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