Guessing game getting while loop keeps repeating.

Posted 14 January 2013 - 08:31 AM

import random
number = random.randint(1,20)
guess=raw_input ("Guess the number I'm thinking of between 1 and 20")
while True:
if guess > number:
print "No, the answer is less than ",guess
elif guess < number:
print "No, the answer is greater than ",guess
elif guess == number:
print "Yes, you guessed correctly! The answer is ",number

This is my current code but when i run it the while loop constantly repeats itself and i cant get out of it. Any tips?

Re: Guessing game getting while loop keeps repeating.

Posted 19 January 2013 - 07:20 AM

What darek9576 said - or I'll just tell you, cause it's not a long explanation. break breaks out of loops. There, that's it. So you just put "break" in your code, wherever you want to exit the loop. Like this:

import random
while True:
print("I'm stuck in a loop!")
if random.randrange(0,10) == 4:
break
print("Yay, I got out of the loop")