Hi, Im creating a simple program. I just need users input and to continue the flow of the program (using if,else and while loops)

Question:

playerChoice = input("Do you want to play more (y/n) ? ").lower()
while playerChoice != "y" or "n":
    playerChoice = input("Do you want to play more (y/n) ? ").lower()

if playerChoice == "y":
    nextWord = int(input("Well then, which word would you like to go next (1/2/3/4) ? "))
    while nextWord != 1 or 2 or 3 or 4:
        nextWord = int(input("Well then, which word would you like to go next (1/2/3/4) ? "))
    if nextWord == 1:
        print("I'm 1")
    elif nextWord == 2:
        print("I'm 2")
    elif nextWord == 3:
        print("I'm 3")
    elif nextWord == 4:
        print("I'm 4")
elif playerChoice == "n":
    print("Thanks for playing, Bye!")
    exit()

So basically, this program woud’nt run pass my (Do you want to play more (y/n) ? ) statement, its keep repeating it no matter the users output. How do I fix it?

This is all im getting:

  Do you want to play more (y/n) ? y
  Do you want to play more (y/n) ? y
  Do you want to play more (y/n) ? y
  Do you want to play more (y/n) ? y
  Do you want to play more (y/n) ? y
  Do you want to play more (y/n) ? y
  Do you want to play more (y/n) ? y
  Do you want to play more (y/n) ? 
Asked By: Mathen

||

Answers:

The condition playerChoice != "y" or "n" is the same as (playerChoice != "y") or "n".

It checks if playerChoice != "y" is true, or if "n" is true. And "n" will always be true, hence the infinite loop.

You need for example playerChoice not in ["y", "n"] instead.

Your code was not properly made

playerChoice = input("Do you want to play more (y/n) ? ").lower()
while True:
    if playerChoice == "y":
        nextWord = int(input("Well then, which word would you like to go next (1/2/3/4) ? "))
        while nextWord not in [1,2,3,4]:
            nextWord = int(input("Invalid! Please select only one of these options, which word would you like to go next (1/2/3/4) ? "))
        if nextWord == 1:
                print("I'm 1")
        elif nextWord == 2:
            print("I'm 2")
        elif nextWord == 3:
            print("I'm 3")
        elif nextWord == 4:
            print("I'm 4")
    elif playerChoice == "n":
        print("Thanks for playing, Bye!")
        exit()
    playerChoice = input("Do you want to play more (y/n) ? ").lower()
Answered By: Mohit Kumar

First off your indentation is not correct, the code needs to execute under the while loop. You can set the playerchoice variable to nothing before running the loop and only check if n is in the answer, then exit or process the input accordingly depending on the input. Example:

playerChoice = ""
while "n" not in playerChoice:
    playerChoice = input("Do you want to play more (y/n) ? ").lower()
    if playerChoice == "y":
        nextWord = int(input("Well then, which word would you like to go next (1/2/3/4) ? "))
        while nextWord not in [1,2,3,4]:
            nextWord = int(input("Invalid input, which word would you like to go next (1/2/3/4) ? "))
        if nextWord == 1:
            print("I'm 1")
        elif nextWord == 2:
            print("I'm 2")
        elif nextWord == 3:
            print("I'm 3")
        elif nextWord == 4:
            print("I'm 4")
    elif playerChoice == "n":
        print("Thanks for playing, Bye!")
        break

Test:

Do you want to play more (y/n) ? y
Well then, which word would you like to go next (1/2/3/4) ? 2
I'm 2
Do you want to play more (y/n) ? y
Well then, which word would you like to go next (1/2/3/4) ? 5
Invalid input, which word would you like to go next (1/2/3/4) ? 4
I'm 4
Do you want to play more (y/n) ? n
Thanks for playing, Bye!

I changed the nextword loop to only check if the inputs 1,2,3,4 is being entered by putting the values in a list and checking if the input is in that.

Answered By: user56700
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.