Python 3: how to compare multiple strings in one line of code?

Question:

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")

I don’t understand the syntax for having it check if any of category1, category2, category3, etc is true/false

Asked By: user2353283

||

Answers:

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


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

Answered By: Matt Ball
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.