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()
3 Answers 3
You define number_guessed as False, so the loop does not execute at all. Try while not number_guessed.
4 Comments
True, and some event inside the loop should set it to False. If you don't have this, you'll be looping forever!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).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
1 Comment
not number_guessed would be a lot better than number_guessed == False.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()