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

2024-10-01 11:24:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在python3中进行一个category select(通过文本接口),我想知道如果多个字符串不正确,如何进行比较,然后按照“这不是一个有效的选择”这样的行打印一些内容

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

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


Tags: 字符串文本内容inputyourifisselect
1条回答
网友
1楼 · 发布于 2024-10-01 11:24:00

Use ^{} 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


顺便说一下,如果您使用的是python2,那么您应该使用raw_input而不是{}。在

相关问题 更多 >