# need help with loops

Page 1 of 1

## 4 Replies - 1050 Views - Last Post: 06 July 2009 - 04:58 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=113242&amp;s=5ca50ff9aeea84167df864ee5ecd2538&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Solris

Reputation: 0
• Posts: 44
• Joined: 09-November 08

# need help with loops

Posted 05 July 2009 - 01:30 PM

Hi all,

I'm new to python. Just started to learn about loops from this book im learning from. I created this small program, now I'm tryint to add a loops to it, so the program will allow the user three tries to guess the right number, but it seems that Im either using the right loop teqniue or not fully understanding how a loop works all together. Can someone help?

Here's my code

```import random
counter = 3
print ("let see if your smart enough guess the number I'm thinking of. You get 3 tries?")
number = number.randint(1,3)
for guess in range (3):
guess = int(input("Take a guess:"))
if guess > number:
print ("Your guess is too high, try again")
elif guess < number:
print ("Your guess is too low, try again")
break
counter = counter-1
elif guess == number:
print ("dam, you guessed my number")

```

Is This A Good Question/Topic? 0

## Replies To: need help with loops

### #2 Stutzbach

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

## Re: need help with loops

Posted 05 July 2009 - 03:33 PM

Solris, on 5 Jul, 2009 - 02:30 PM, said:

I'm new to python. Just started to learn about loops from this book im learning from. I created this small program, now I'm tryint to add a loops to it, so the program will allow the user three tries to guess the right number, but it seems that Im either using the right loop teqniue or not fully understanding how a loop works all together. Can someone help?

Are you getting an error, or does the program do something you don't expect?
If you get an error, what's the error message?
If the program doesn't do what you'd expect, what is it doing instead?

In the code you posted, is the indication correct? It looks like it would raise a SyntaxError.

### #3 Solris

Reputation: 0
• Posts: 44
• Joined: 09-November 08

## Re: need help with loops

Posted 05 July 2009 - 03:47 PM

Hi Stutzbach,

Thanks for your reply. A syntax error does occur. The error apperars where I declare guess. There was no error before in the program, till i attempted to add loops to the program.

The syntax error is "expected an indednted block"

Sorry i didn't mention that error before, i just left it as is since i knew it was the loop i was trying to add that was causing the error.

### #4 Stutzbach

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

## Re: need help with loops

Posted 05 July 2009 - 05:50 PM

Solris, on 5 Jul, 2009 - 04:47 PM, said:

The syntax error is "expected an indednted block"

That makes sense.

You need to indent the code that you want to be executed repeatedly (much like you indent the code after an "if" statement), otherwise Python has no way to know which bits to repeat.

Hope that helps!

### #5 code_m

Reputation: 24
• Posts: 202
• Joined: 21-April 09

## Re: need help with loops

Posted 06 July 2009 - 04:58 AM

Unlike other languages, python doesn't allow defined blocks, instead you must specify what goes with what with indenting.

Your syntax error is occurring because you have no indentations after your for loop. Note that python outputs the line number on which the Error occurred, so it's not too bad to debug. (sometimes it's not exact, like when you forget the closing parentheses for a function call, the error actually occurs on the following line)

To get use to forced indenting, try running within the interpreter, you should be able to pick up on Errors faster (that's why it's there!)

This post has been edited by code_m: 06 July 2009 - 05:00 AM