Elijah Anderson
Elijah Anderson

Reputation: 39

While loop with multiple conditions

So my while loop just keeps looping even when it shouldn't, if there is only 1 condition the loop works and then proceeds to the next lines of code but when I add the OR statement it wont work, I'm sure it's something very silly but I'm only a beginner and have tried researching this.

Choice = input("What would you like to do? New Game, Continue, or Quit?").upper()
while Choice != "NEW GAME" or Choice != "QUIT":
    print ("That input was invalid, please try again.")
    Choice = input("What would you like to do? New Game, Continue, or Quit? ").upper()
    if Choice == "QUIT":
        quit()

Upvotes: 2

Views: 2109

Answers (3)

Craig Burgler
Craig Burgler

Reputation: 1779

I think you are looking for

while Choice not in ["New Game", "Continue", "Quit"]

or better to allow for alternative capitalization:

while Choice.upper() not in ["NEW GAME", "CONTINUE", "QUIT"]

Also please uncapitalize the variable Choice. When other Python programmers see a variable that starts with a capital letter they assume at first that it is a class name.

Upvotes: 2

Greg Hewgill
Greg Hewgill

Reputation: 993095

The condition

Choice != "NEW GAME" or Choice != "QUIT"

will always be True. Any value of Choice will be either not "NEW GAME" or not "QUIT". Instead, use:

Choice != "NEW GAME" and Choice != "QUIT":

Upvotes: 6

Arkanosis
Arkanosis

Reputation: 2334

I guess you want a and there, not a or

Because Choice can't be at the same time not different from "NEW GAME" and from "QUIT". In other words, your loop condition is always True whatever the value of Choice is.

Upvotes: 0

Related Questions