First python program, syntax error in while loop

Zachary Ware zachary.ware+pylist at gmail.com
Fri May 3 13:37:39 EDT 2013


On Fri, May 3, 2013 at 12:18 PM,  <ryankoch38 at gmail.com> wrote:
> title = "Guess my number game:"
> print title.title()
> raw_input("Press any key to continue..")
>
> import random
>
> tries = 0
> number = random.randrange(99) + 1
> guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")

First up, there's a missing ) on each call to int(), which is causing
the syntax error you see.   The error points at "while" because
"while" doesn't make sense in the call to int().

>
> while (guess != number):
> if (guess > number):

After fixing the error above, you'll have another one here: you
haven't indented the block to be executed in the while loop.
Indentation is important in Python, it delimits code blocks and makes
things more readable for people.

>     number = int(raw_input("Sorry, my number is lower than that! \n Try again:")
>    tries += 1

There's also another indentation error here: everything in the same
block has to be indented to the same level.

> else if (guess < number):
>     number = int(raw_input("Sorry, my number is higher than that! \n Try again:")
>     tries += 1
> print "Congratulations, you guessed my number! \n And it only took you" tries "tries!"

And here will be another syntax error: you have to separate your
arguments to 'print' with commas.

>
> raw_input("\n\n Press any key to exit..")
>
> ## what is wrong with this script? I'm just trying to understand while loops and ## this is not a real project :P


Once you have those errors fixed, I'll bet I can guess your number on
the second try every time.  I'll let you figure out why yourself,
though ;).



More information about the Python-list mailing list