6

I'm new to python and I'm trying to make a simple Guess the number game, and I'm stuck in an if statement in a while loop. here is the code.

I'm experiencing it at the Your guess it too high and the low one. I tried breaking it, but it simply stops the whole things

def guess_the_number():
 number = random.randrange(20)
 guessesMade = 0
 print('Take a guess')
 guess = input()
 guess = int(guess)
 while guessesMade < 6:
 if guess < number:
 print('Your guess is too low.')
 if guess > number:
 print('Your guess is too high.')
 if guess == number:
 break
 if guess == number:
 print'You got it in', guessesMade, 'guess(es)! Congratulaions!'
 else:
 print'I\'m sorry, the number was', number
asked Jun 3, 2015 at 12:00

1 Answer 1

7

You never increment guessesMade so guessesMade < 6 will always be True. You need to modify this value within your loop. You also need to move your prompt for user input into the loop

while guessesMade < 6:
 guess = int(input('Take a guess'))
 if guess < number:
 print('Your guess is too low.')
 guessesMade += 1
 elif guess > number:
 print('Your guess is too high.')
 guessesMade += 1
 else:
 break
answered Jun 3, 2015 at 12:02
Sign up to request clarification or add additional context in comments.

3 Comments

I have tried that already, it just prints it all again and doesn't ask the number
@Kasra I'm a time-traveler from 60 seconds in the future, so I get a head start!
guess cannot be three things at once

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.