import random
def game():
number = random.randint(0, 10)
guess = 0
print("Guess a number from 0-10:")
while number != guess:
try:
guess = int(input(""))
if number != guess:
print("you haven't guessed the number, keep trying")
else:
print("You guessed it!")
break
except ValueError:
print("Please enter an integer")
game()
choose = input("Would you like to play again?\n")
while choose == "yes":
game()
if choose == "no":
break
I'm trying to add a feature where every time the game is won, the user has the option to play again, right now the game runs, then you win, it asks if you want to play again, you say yes, it runs again then you win and it runs again without asking.
You're currently only asking the user if he wants to play again once , and keep it going with the while
loop. You should ask the user again after every time the game is played, like so:
choose = input("Would you like to play again?\n")
while choose == "yes":
game()
choose = input("Would you like to play again?\n") #add this line
if choose == "no":
break
Choose is only set one time, so the while-loop never breaks. You could simply add:
choose = input("Would you like to play again?\n")
while choose == "yes":
game()
choose = input("Would you like to play again?\n")
if choose == "no":
break
Or somewhat more elegantly:
choose = input("Would you like to play again?\n")
while choose != "no":
game()
choose = input("Would you like to play again?\n")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.