0

In this code, the user is to guess a number the computer has chosen randomly between 0 and 100. The problem is that the while loop doesn't get executed at all. Everything was working until I put that code block into the while loop to get it repeated until the user guesses the number or runs out of attempts. How do I get the while loop to work? Please I am a beginner in python.

import random
def guessing_game():
 print('''Welcome to the Number Guessing Game!
I'm thinking of a number between 1 and 100.''')
 select_level = input("Choose a difficulty. Type 'easy' or 'hard': easy: ")
 if select_level == "easy":
 attempt_left = 10
 print("You have 10 attempts remaining to guess the number.")
 elif select_level == "hard":
 attempt_left = 5
 print("You have 5 attempts remaining to guess the number.")
 computer_choice = random.randint(0,100)
 #print(f"Pssst, the correct answer is {computer_choice}")
 number_guessed = False
 while number_guessed:
 user_choice = int(input("Please enter a number between 0 and 100: "))
 if computer_choice == user_choice:
 number_guessed = True
 print(f"You got it! The answer was {computer_choice}")
 else:
 attempt_left -= 1
 if user_choice > computer_choice:
 print(f"That is too high!\nGuess again.\nYou have {attempt_left} attempts remaining to guess the number.")
 else:
 print(f"That is too low!\nGuess again.\nYou have {attempt_left} attempts remaining to guess the number.")
 if attempt_left == 0:
 number_guessed = True
 print("You've run out of guesses, you lose.")
guessing_game()
asked Mar 18, 2022 at 23:38
0

3 Answers 3

2

You define number_guessed as False, so the loop does not execute at all. Try while not number_guessed.

answered Mar 18, 2022 at 23:43
Sign up to request clarification or add additional context in comments.

4 Comments

while not number_guessed indeed solved the problem but it got confused about the use of flags with while loop. Does this mean a flag should always be set as True?The while loop should keep running until number_guessed is True (the user got the correct number hence the game should stop)
You always want to start a loop with the condition being True, and some event inside the loop should set it to False. If you don't have this, you'll be looping forever!
while not number_guessed indeed solved the problem but it got confused about the use of flags with while loop. Does this mean a flag should always be set as True?The while loop should keep running until number_guessed is True (the user got the correct number hence the game should stop) I set the number_guessed as False ... so if I then write while not number_guessed, I expected that the flag will not be set as True. And if True, then it means the user has guessed the number correctly so the game should end, which isn't the case. Why am I confused huh?
@Sethoo: The flag variable can work either way. The condition (which can either be just the flag, or an expression like not flag) needs to be True if the loop should keep running (including running the very first time). You can name your variables whichever way you want, the cost of a not expression is trivial (compared to other stuff like IO).
1

This should work:

import random
def guessing_game():
 print('''Welcome to the Number Guessing Game!
I'm thinking of a number between 1 and 100.''')
 select_level = input("Choose a difficulty. Type 'easy' or 'hard': easy: ")
 if select_level == "easy":
 attempt_left = 10
 print("You have 10 attempts remaining to guess the number.")
 elif select_level == "hard":
 attempt_left = 5
 print("You have 5 attempts remaining to guess the number.")
 computer_choice = random.randint(0,100)
 #print(f"Pssst, the correct answer is {computer_choice}")
 number_guessed = False
 while number_guessed == False:
 user_choice = int(input("Please enter a number between 0 and 100: "))
 if computer_choice == user_choice:
 number_guessed = True
 print(f"You got it! The answer was {computer_choice}")
 else:
 attempt_left -= 1
 if user_choice > computer_choice:
 print(f"That is too high!\nGuess again.\nYou have {attempt_left} attempts remaining to guess the number.")
 else:
 print(f"That is too low!\nGuess again.\nYou have {attempt_left} attempts remaining to guess the number.")
 if attempt_left == 0:
 number_guessed = True
 print("You've run out of guesses, you lose.")
guessing_game()

The error with your code was that when you use a while loop like while somevariable and somevariable equals False, the while loop will not run. You could also just try while not number_guessed

answered Mar 18, 2022 at 23:42

1 Comment

not number_guessed would be a lot better than number_guessed == False.
-1

The numbers_guessed is false, and the while loop must be true to run. So change the while loop or the variable.

Code:

import random
def guessing_game():
 print('''Welcome to the Number Guessing Game!
I'm thinking of a number between 1 and 100.''')
 select_level = input("Choose a difficulty. Type 'easy' or 'hard': easy: ")
 if select_level == "easy":
 attempt_left = 10
 print("You have 10 attempts remaining to guess the number.")
 elif select_level == "hard":
 attempt_left = 5
 print("You have 5 attempts remaining to guess the number.")
 computer_choice = random.randint(0,100)
 #print(f"Pssst, the correct answer is {computer_choice}")
 number_guessed = False
 while not number_guessed:
 user_choice = int(input("Please enter a number between 0 and 100: "))
 if computer_choice == user_choice:
 number_guessed = True
 print(f"You got it! The answer was {computer_choice}")
 else:
 attempt_left -= 1
 if user_choice > computer_choice:
 print(f"That is too high!\nGuess again.\nYou have {attempt_left} attempts remaining to guess the number.")
 else:
 print(f"That is too low!\nGuess again.\nYou have {attempt_left} attempts remaining to guess the number.")
 if attempt_left == 0:
 number_guessed = True
 print("You've run out of guesses, you lose.")
guessing_game()
answered Mar 18, 2022 at 23:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.