i'm hoping the title is self-explanatory, but for more context: i wanna code a random number guessing game and instead of the game ending when the user guesses correctly, i ask the player if they'd like to play again and in the scenario where they answer yes, the code generates a new number and the game repeats.
this is the code:
# import time and random values
import random
import time
# tracks the number of attempts
attempts = 0
#introduction
print("welcome to my number guessing game! ")
input("press any key to start playing ")
while True:
# ask user for input
number1 = random.randint(0,10)
guess = int(input("guess a random number between 0 and 10. "))
attempts += 1
# verify if guess is correct
if guess == number1:
playAgain = input(f"correct! the secret number was indeed {number1}, and you guessed it in {attempts} attempts. play again? (yes/no) ")
if playAgain == "yes":
# generate a new number and make the user try again
number1 = random.randint(0,10)
continue
else:
break
elif guess > number1:
print("try a smaller number. ")
else:
print("try a bigger number. ")
instead of only generating a new number once the user guesses correctly and decides to play again, it generates a new number while the user is playing and so the game never really ends. how do i fix this?
change ur
while True:
# ask user for input
number1 = random.randint(0,10)
guess = int(input("guess a random number between 0 and 10. "))
attempts += 1
to
while True:
# Generate a new number for each game
number1 = random.randint(0, 10)
while True:
# ask user for input
guess = int(input("Guess a random number between 0 and 10: "))
attempts += 1