[Tutor] Guess my number game

Steven D'Aprano steve at pearwood.info
Sun Dec 8 03:13:05 CET 2013


On Fri, Dec 06, 2013 at 11:34:13PM +0000, Lelani Slabber wrote:

> I have to add code so the user has a limited number of tries - in this 
> case I have set it to less than 5 in the while loop and I want the 
> program to stop if the tries are equal to 5.  I get an invalid syntax 
> error.  Please help.

Are we playing "Guess My Error" now? Yay, I love that game!

Actually I don't. Please in future copy and paste the entire error 
message you get. It usually shows very useful information, such as the 
actual line causing the error.

In this case I can guess that you're probably getting a syntax error on 
the following:

[...]
>             tries=tries+1
>             print("too low")
>             guess = int(input("Take a guess: "))
>             else:

The else is not paired with any if. else, like elif, can only follow an 
if, and must be indented to the same level:

if something:
    indented block
elif something else:
    indented block
else:
    indented block


You can have any number of "elif" blocks per "if", but no more than one 
"else" block per "if".

-- 
Steven


More information about the Tutor mailing list