+ 1

Trying to make Guessing number game

I am a beginner in learning python 3. I am trying to make the guessing number game to increase my understanding in python. However I find some problem in the coding. Can u guys improve or tell me where did I go wrong here: 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("Number has been generated. You can make a guess now.")) while guessing_section: if player_guess>result: print("Your guess is too high.") elif player_guess<result: print("Your guess is too low.") elif player_guess==result: print("Congratulation! Your answer is correct") play_again=input("Do you want to play again? (Yes or no)") if play_again=="yes": generating_number=True elif play_again=="no": generating_number=False print("Bye-bye. It is fun to play with you") break

16th May 2020, 8:03 AM
Muhammad Hafiz Aiman bin Misnadi
Muhammad Hafiz Aiman bin Misnadi - avatar
4 Answers
+ 1
If response == "no" Then guessing_section == False and the line of: player_guess = int(input()) Will NEVER execute, so when you use it afterwise, it will cause NameError You can fix this by adding (and generating_number) To while guessing session, this will look like : while guessing_section and generating_number: if player_guess>result: ... And so on
16th May 2020, 2:10 PM
NourEddine Yassine
0
the error that I get is player_guess is not defined
16th May 2020, 8:06 AM
Muhammad Hafiz Aiman bin Misnadi
Muhammad Hafiz Aiman bin Misnadi - avatar
0
You get this error when response=="no" ?
16th May 2020, 8:13 AM
NourEddine Yassine
0
NourEddine Yassine yup exactly. Why did it turn out like that?
16th May 2020, 10:37 AM
Muhammad Hafiz Aiman bin Misnadi
Muhammad Hafiz Aiman bin Misnadi - avatar