Search code examples
pythonstringcomparator

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


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


Solution

  • 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.