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) ?
CodePudding user response:
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.
CodePudding user response:
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 !=5:
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")
nextWord = int(input("Well then, which word would you like to go next (1/2/3/4) ? "))
elif playerChoice == "n":
print("Thanks for playing, Bye!")
exit()
playerChoice = input("Do you want to play more (y/n) ? ").lower()
CodePudding user response:
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.