Python 3:如何在一行代码中比较多个字符串?
问题描述:
我正在尝试在Python 3中选择类别(通过文本界面),我想知道如何比较多个字符串是否不正确,然后沿那是无效的行打印内容选择
I'm trying to make a category select (by text interface) in Python 3, and I was wondering how I can compare if multiple strings are not true, and then print something along the lines of "that is not a valid choice"
input("what is your category choice?")
if categoryChoice != "category1", "category 2", "category 3":
print("not a valid choice")
我不明白让它检查category1,category2,category3等是否为真/假的语法
I don't understand the syntax for having it check if any of category1, category2, category3, etc is true/false
答
categoryChoice = input("what is your category choice?")
if categoryChoice not in ("category1", "category 2", "category 3"):
print("not a valid choice")
如果您是使用Python 2,您应该真的使用 raw_input
而不是 input
。
By the way, if you're using Python 2, you should really use raw_input
instead of input
.