Where did i went wrong here | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Where did i went wrong here

After player guess it correctly. They are ask if the want to play again or not. If the type no, the it print bye bye and programme end. But If they say yes the programme ends. I want to make it loop to the beginning of game. The code as below import random import time minimum=1 maximum=10 generating_number=True guessing_section=True print("The Guessing Number Game") print("Computer will generate a number and you have to guess it. \nThe range is 1 to 10") response=input("Do you want to play? (Answer yes or no)") while response not in ["yes","no"]: response=input("Invalid input. Answer with yes or no :") if response== "yes": generating_number==True elif response=="no": generating_number=False print("Bye-bye dear friend") while generating_number: print("Game Starts") print("Generating a number.....") time.sleep(random.randint (1,5))#wait between 1 to 4 seconds randomly result=random.randint(minimum, maximum) print(result) generating_number=False player_guess =int(input("Do your best to guess.")) while guessing_section: if player_guess>result: print("Your guess is too high.") player_guess =int(input("Try again")) continue elif player_guess<result: print("Your guess is too low.") player_guess =int(input("Try again")) continue elif player_guess==result: print("Congratulation! Your answer is correct") break play_again=input("Do you want to play again? (Yes or no)") while play_again not in ["yes","no"]: play_again=input("Invalid input. Answer with yes or no") if play_again=="yes": generating_number==True continue #I want when the user say yes here, there will loop back to beginning of game elif play_again=="no": generating_number=False print("Bye-bye. It is fun to play with you")

18th May 2020, 7:42 AM
Muhammad Hafiz Aiman bin Misnadi
Muhammad Hafiz Aiman bin Misnadi - avatar
1 Answer
0
For this i like to do a recursive function. Id wrap whatever i want to repeat endlessly in a function, then call it when needed. def play(): print('Lets play!') num = random.randint(1,10) numfound = False while not numfound: guess = int(input('Take a guess > ')) if guess == num: print('Got it!') numfound = True choice = input('Play again [y/n] ?') if choice == 'y': <------------- play() <------------ else: print('Bye!') else: print('Try again') play()
18th May 2020, 7:59 AM
Slick
Slick - avatar