Home > Net >  while loop is not being executed
while loop is not being executed

Time:03-19

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()

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

I changed my flag name as below it is logical for me now. Thanks, everyone

 number_not_guessed = True
    while  number_not_guessed:
        user_choice = int(input("Please enter a number between 0 and 100: "))
        if computer_choice == user_choice:
            number_not_guessed = False
            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_not_guessed = False
                print("You've run out of guesses, you lose.")

CodePudding user response:

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()
  • Related