Home > Enterprise >  How to stop guessing game after correct input in Python?
How to stop guessing game after correct input in Python?

Time:03-16

I tried to make a guessing game for my tutorial, and it doesn't stop asking inputs even after correct number guessed. can anyone help?

''' #asking user to guess the hidden number in 5 attempts

import random
hidden = random.randint(1,20)
win = False
attempt = 0
total_attempt = 5

while attempt < total_attempt:
     while win == False:
          guess = int(input('Guess the Number:'))
          attempt  = 1

          if hidden == guess:
             print ('You got it in', attempt, 'Guesses')
             win == True
             break
          else:
               if hidden > guess:
                   print ('Your guess is too low')
               else:
                   print ('Your guess is too high')
               break
print ('Not guessed,The correct answer was', hidden)

'''

CodePudding user response:

Your break only breaks the inner while loop, not the outer one. While you could do tricky stuff with a flag that allows the inner loop to signal the need for a break to the outer loop, the simplest fix is to use only a single loop, and use conditional breaks that allow you to stop it for different reasons:

import random
hidden = random.randint(1,20)
attempt = 0
total_attempt = 5

while True:
    guess = int(input('Guess the Number:'))
    attempt  = 1
    if hidden == guess:
        print ('You got it in', attempt, 'Guesses')
        break
    if attempt >= total_attempt:
        print ('Not guessed,The correct answer was', hidden)
        break
    if hidden > guess:
        print ('Your guess is too low')
    else:
        print ('Your guess is too high')

Another option might be to use a for loop over the range of attempts, with a break to end it early for a win and an else to indicate the loss when the loop is exhausted:

import random
hidden = random.randint(1,20)

for attempt in range(5):
    guess = int(input('Guess the Number:'))
    if hidden > guess:
        print('Your guess is too low')
    elif hidden < guess:
        print('Your guess is too high')
    else:
        print('You got it in', attempt   1, 'guesses')
        break  # ends the loop and skips its "else"
else:
    print('Not guessed,The correct answer was', hidden)

CodePudding user response:

The problem is quite easy to find & correct in your code. On line 12, you have the following command : win == True. However, the operator == doesn't change the value of the variable, it's only used to verify and equality and return a boolean. What you want in your code is to change the value of the boolean variable win, not just verify it. Therefore, your code would need to look like this :

import random
hidden = random.randint(1,20)
win = False
attempt = 0
total_attempt = 5

while attempt < total_attempt:
     while win == False:
          guess = int(input('Guess the Number:'))
          attempt  = 1

          if hidden == guess:
             print ('You got it in', attempt, 'Guesses')
             win = True
             exit(0)
          else:
               if hidden > guess:
                   print ('Your guess is too low')
               else:
                   print ('Your guess is too high')
               break
print ('Not guessed,The correct answer was', hidden)

Please not that I added the exit(0) command in case the user finds out the correct number, which will just make the Python script stop.

  • Related