# Guessing game getting while loop keeps repeating.

Page 1 of 1

## 4 Replies - 1307 Views - Last Post: 20 January 2013 - 11:52 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=307198&amp;s=094f4c59a203489a79c7c44499be21e7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 taymo

Reputation: 0
• Posts: 1
• Joined: 14-January 13

# 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?

Is This A Good Question/Topic? 0

## Replies To: Guessing game getting while loop keeps repeating.

### #2 tlhIn`toq

Reputation: 5871
• Posts: 12,790
• Joined: 02-June 10

## Re: Guessing game getting while loop keeps repeating.

Posted 14 January 2013 - 10:27 AM

while True:

Hello? True will always be true. When would it ever stop?

### #3 darek9576

• D.I.C Lover

Reputation: 200
• Posts: 1,697
• Joined: 13-March 10

## Re: Guessing game getting while loop keeps repeating.

Posted 14 January 2013 - 12:36 PM

Look into break statements. If you type into google High-Low game in Python, you will get many results.

### #4 Tayacan

• D.I.C Regular

Reputation: 145
• Posts: 275
• Joined: 18-January 11

## 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")

```

### #5 alexr1090

Reputation: 44
• Posts: 125
• Joined: 08-May 11

## Re: Guessing game getting while loop keeps repeating.

Posted 20 January 2013 - 11:52 AM

or just change the while loop to
```
while guess != number:
...
...
print "Yes, you guessed correctly! The answer is ",number #this will get entered after guess == number, so I took it out of the while loop
```

This post has been edited by alexr1090: 20 January 2013 - 11:52 AM