# Something isn't right with this code

Page 1 of 1

## 1 Replies - 1266 Views - Last Post: 14 September 2008 - 01:00 PMRate 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=63744&amp;s=56785da94f45a0e48acbe1d7d888a409&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 akarren

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 14-September 08

# Something isn't right with this code

Posted 14 September 2008 - 12:19 PM

I'm trying to make the guessing game stop at 5 tries which it is doing but for some reason it isn't printing you fail when the user doesn't guess the number in 5 tries
```# Adam Karren
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money, but the user only has 5 guesses

print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100."
print "Try to guess the number quick, you only got 5 chances."

import random

# set the intial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1

# guessing loop
while (guess != the_number):
if (guess > the_number):
print "Lower..."
else:
print "Higher..."

guess = int(raw_input("Take a guess: "))
tries +=1
if tries == 5:
break
print "You fail"
print "You guessed it! The number was", the_number

raw_input("\n\nPress the enter key to exit")

```

Is This A Good Question/Topic? 0

## Replies To: Something isn't right with this code

### #2 Stutzbach

• New D.I.C Head

Reputation: 6
• Posts: 29
• Joined: 06-July 08

## Re: Something isn't right with this code

Posted 14 September 2008 - 01:00 PM

akarren, on 14 Sep, 2008 - 01:19 PM, said:

```	if tries == 5:
break
print "You fail"

```

The "break" causes it to jump out of the loop, so the print statement is never executed. Put the print statement before the break and it should work as desired.
Was This Post Helpful? 1

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }