How to test that variable is not equal to multiple things? Python

Posted by M830078h on Stack Overflow See other posts from Stack Overflow or by M830078h
Published on 2012-09-23T15:30:57Z Indexed on 2012/09/23 15:37 UTC
Read the original article Hit count: 156

Filed under:

This is the piece of code I have:

choice = ""

while choice != "1" and choice != "2" and choice != "3": 
    choice = raw_input("pick 1, 2 or 3")

    if choice == "1":
        print "1 it is!"

    elif choice == "2":
        print "2 it is!"

    elif choice == "3":
        print "3 it is!"

    else:
        print "You should choose 1, 2 or 3"

While it works, I feel that it's really clumsy, specifically the while clause. What if I have more acceptable choices? Is there a better way to make the clause?

© Stack Overflow or respective owner

Related posts about python