4 Replies - 1032 Views - Last Post: 06 July 2009 - 04:58 AM Rate Topic: -----

#1 Solris  Icon User is offline

  • New D.I.C Head

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

need help with loops

Post icon  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  Icon User is offline

  • New D.I.C Head

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

Re: need help with loops

Posted 05 July 2009 - 03:33 PM

View PostSolris, 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.
Was This Post Helpful? 0
  • +
  • -

#3 Solris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 Stutzbach  Icon User is offline

  • New D.I.C Head

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

Re: need help with loops

Posted 05 July 2009 - 05:50 PM

View PostSolris, 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!
Was This Post Helpful? 0
  • +
  • -

#5 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1