user3347937
user3347937

Reputation:

Python multiple comparison for string

    shift = raw_input("Enter the shift the employee works in:  ")
    shift = shift.upper()
    print shift
    while (shift != "A" or shift != "B" or shift != "C" ):
        shift = raw_input("Invalid Input- Please Enter a, b or c:  ")
        shift = shift.upper()

I need to validate that the user is choosing "a, b or c" I also must use string.upper(). However, it keeps going into the while loop even when I input "a, A, b, B, c or, C" I have the "print shift" to make sure it's inputing correctly and it is.

When I only have "shift != "A"" and type in "a or A" it won't go into the loop. It's only when I add the "B and C" that it starts to mess up. How do I fix this?

Upvotes: 3

Views: 623

Answers (1)

perreal
perreal

Reputation: 98068

You need to use and instead of or (because x != 1 or x != 2 is always true):

while (shift != "A" and shift != "B" and shift != "C" ):

But you can do better:

while shift not in "ABC":

Upvotes: 2

Related Questions