0

I am trying to teach myself basic python scripting and create the number guessing game, but I am running into an infinite loop for my while loop. The program will infinitely print the first print statement from the initial entry.

#!/usr/bin/python
import random

print("Here is a guessing game!")
print("Try to guess the number. It is a value from 1-100")

answer = random.randint(1,100)
guess = int(input('Enter your guess: '))
counter = 0

while (guess != answer):
   counter += 1
   if guess < answer:
       print('Your guess is too low!')
   elif guess > answer:
       print('Your guess is too high!')
   else:
       print('Correct!')
       print('It took you' + count + ' guesses.')
1
  • 2
    Either guess or answer has to be modified inside loop. Here, the guess input should be inside loop so it asks everytime what's your next guess. Commented Sep 10, 2019 at 3:20

2 Answers 2

2
#Try asking the guess again in each loop until the correct answer and print the counter outside the loop

#!/usr/bin/python
import random

print("Here is a guessing game!")
print("Try to guess the number. It is a value from 1-100")

answer = random.randint(1,100)
guess = int(input('Enter your guess: '))
counter = 0

while (guess != answer):
   counter += 1
   if guess < answer:
       print('Your guess is too low!')
   elif guess > answer:
       print('Your guess is too high!')
   guess = int(input('Try again, your new guess: '))
print('Correct!')
print('It took you' + counter + ' guesses.')
Sign up to request clarification or add additional context in comments.

3 Comments

OP does not want to have a user input every iteration of the loop
But without user input, it doesn't make a number guessing game, because if you just guessed once and the loop runs till it randomly gets that exact number you guessed, I don't see a point there.
OP is using a random number? this takes care of the user input every iteration of the loop, hence the user every iteration is invalid
0

User should input a value inside the loop, so I think you need to change the code to this:

import random

print("Here is a guessing game!")
print("Try to guess the number. It is a value from 1-100")

answer = random.randint(1,100)
guess = int(input('Enter your guess: '))
counter = 0

while (guess != answer):
   counter += 1
   if guess < answer:
       print('Your guess is too low!')
       guess = int(input('Enter your guess: '))
   elif guess > answer:
       print('Your guess is too high!')
       guess = int(input('Enter your guess: '))
   else:
       print('Correct!')
       print('It took you' + str(counter) + ' guesses.')

It's because the user input the guess value once, so when it going in to loop, it will loop forever and print (too low or too high)

2 Comments

Similar to comment above, user input in this case is not helpful as the OP is using a random input every time.
Oh, so why the OP counting the guesses? if the user input just once, just print 'It took you 1 guesses.'. I think the random value are the answer, so we have to guess the random number and count it.

Your Answer

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.