Python 3:如何在一行代码中比较多个字符串? [英] Python 3: how to compare multiple strings in one line of code?

查看:125
本文介绍了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")

http://ideone.com/e6n0Rr

如果您是使用Python 2,您应该真的使用 raw_input 而不是 input

By the way, if you're using Python 2, you should really use raw_input instead of input.

这篇关于Python 3:如何在一行代码中比较多个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆