Python 3:如何在一行代码中比较多个字符串?

时间:2013-05-06 03:43:28

标签: python string python-3.x comparator

我正在尝试在Python 3中进行类别选择(通过文本界面),我想知道如果多个字符串不正确我将如何比较,然后打印“不是有效”的行选择“

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

我不明白检查category1,category2,category3等是否为true / false的语法

1 个答案:

答案 0 :(得分:6)

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