2013-05-06 19 views
0

我想在Python 3中做一個類別選擇(通過文本界面),我想知道如何比較多個字符串是否爲真,然後按照「這不是一個有效的選擇「Python 3:如何比較一行代碼中的多個字符串?

input("what is your category choice?") 
    if categoryChoice != "category1", "category 2", "category 3": 
    print("not a valid choice") 

我不明白的語法,有它檢查是否有任何類別1,類別2,類別3,等的就是真/假

回答

5

Use in for containment tests.

categoryChoice = input("what is your category choice?") 
if categoryChoice not in ("category1", "category 2", "category 3"): 
    print("not a valid choice") 

http://ideone.com/e6n0Rr


順便說一句,如果你使用Python 2,你應該真的使用raw_input代替input