I am learning python and trying to complete a Guessing game. here's the code
import random
def guess_function(x):
while True:
number = random.randint(0,x)
userinput = input(f"Guess a number between 0 to {x}: ")
if userinput.lower() == "q":
break
userinput = int(userinput)
if int(userinput) == number:
print("Thats correct")
continue
if userinput < number:
print("TO low ")
if userinput > number:
print("TO high ")
guess_function(100)
but the problem in this code is this generates a new random number every time i make a guess right or wrong, if i declare the number variable outside the while loop then new number wont be generated after i guess the correct number. i want this to continuously run until i press "q". i have tried elif and if both it still generates new number. also tried to add a while loop inside this while loop but that makes it a infinite loop. i want to know how can i have this continuously run and have same random number and once i make right guess the number changes but without ending game, means not having to run the code again.
Aucun commentaire:
Enregistrer un commentaire