First python program, syntax error in while loop

R

ryankoch38

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 :")

while (guess != number):
if (guess > number):
number = int(raw_input("Sorry, my number is lower than that! \n Try again:")
tries += 1
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!"

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
 
C

Chris “Kwpolska†Warrick

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 :")

while (guess != number):
if (guess > number):
number = int(raw_input("Sorry, my number is lower than that! \n Try again:")
tries += 1
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!"

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

1. post full tracebacks.
2. The contents of your while loop must be indented, just like the
contents of the if/else if statement you have there. So, four spaces
before the 'if', 'else if' and 'print' lines; eight before 'number'
and 'tries' lines.
 
J

John Gordon

In said:
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 :")
while (guess != number):
if (guess > number):
number = int(raw_input("Sorry, my number is lower than that! \n Try again:")
tries += 1
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!"
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

You're missing a second closing parentheses on the line where the user
inputs their guess.

Also, you need to indent the 'if' statement underneath the while loop.
 
M

MRAB

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 :")

while (guess != number):
if (guess > number):
number = int(raw_input("Sorry, my number is lower than that! \n Try again:")
tries += 1
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!"

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
Indentation in important in Python.

Also, "else if" should be "elif", and you ask for a guess only once.


title = "Guess my number game:"
print title.title()
raw_input("Press any key to continue..")

import random

tries = 0
number = random.randrange(99) + 1

# Ask for first guess.
guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")

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

# Ask for next guess.
guess = int(raw_input("Guess my number! Secret - It is between 1
and 100 :")

print "Congratulations, you guessed my number! \n And it only took you"
tries "tries!"

raw_input("\n\n Press any key to exit..")
 
Z

Zachary Ware

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 ;).
 
J

John Gordon

In said:
1. post full tracebacks.

I almost responded with the same advice, but in this case the full
traceback doesn't really tell us anything more:

File "foo.py", line 11
while guess != number:
^
SyntaxError: invalid syntax
 
R

ryankoch38

Okay, thank you very much for the timely replies, heres what i have now:

title = "Guess my number game:"
print title.title()
raw_input("Press any key to continue..")

import random

number = random.randrange(99) + 1
tries = 0
guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :"))

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

print "good job, you won!"



raw_input("\n\n Press any key to exit..")


## it seems to stick with "higher" or "lower" after my first guess, whichever it
## is
 
J

John Gordon

In said:
Okay, thank you very much for the timely replies, heres what i have now:
title = "Guess my number game:"
print title.title()
raw_input("Press any key to continue..")
import random
number = random.randrange(99) + 1
tries = 0
guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :"))
while (guess != number):
if (guess > number):
number = int(raw_input("Sorry, my number is lower than that! \n Try again:"))
tries += 1
else:
number = int(raw_input("Sorry, my number is higher than that! \n Try again:"))
tries += 1

print "good job, you won!"


raw_input("\n\n Press any key to exit..")

## it seems to stick with "higher" or "lower" after my first guess, whichever it
## is

When the user re-enters their guess in the while loop, you're assigning
the input to "number" instead of "guess".
 
R

ryankoch38

I've got it working! I'm really enjoying python :) But now i'd like to make it break out of the while loop when the user guesses more than 5 numbers and fails..

title = "Guess my number game:"
print title.title()
raw_input("Press any key to continue..")

import random

number = random.randrange(99) + 1
tries = 0
guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :"))

while (guess != number):
if (guess > number):
guess = int(raw_input("Sorry, my number is lower than that! \n Try again:"))
tries += 1
if (guess < number):
guess = int(raw_input("Sorry, my number is higher than that! \n Try again:"))
tries += 1
if (tries > 5):
print "Sorry, you lost!"
break

print "\nCongratulations! You guessed my number in", tries, "tries"


raw_input("\n\n Press any key to exit..")

## this results in the congratulations printing after the second/third guess and ## continuing
 
J

John Gordon

In said:
I've got it working! I'm really enjoying python :) But now i'd like to make it break out of the while loop when the user guesses more than 5 numbers and fails

The "Congratulations" message is inside the while loop; that's why it
always prints.

To break after five guesses, you could change the while loop to this:

while (guess != number) and (tries < 5):

And then after the while loop, put this if statement:

if (tries < 5):
print "\nCongratulations! You guessed my number in", tries, "tries"

else:
print "Sorry, you lost!"
 
R

ryankoch38

Thank you! It's 100% functional now, here's the final project:

title = "Guess my number game:"
print title.title()
raw_input("Press any key to continue..")

import random

number = random.randrange(99) + 1
tries = 0
guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :"))

while (guess != number) and (tries <5):
if (guess > number):
guess = int(raw_input("Sorry, my number is lower than that! \n Try again:"))
tries += 1
if (guess < number):
guess = int(raw_input("Sorry, my number is higher than that! \n Try again:"))
tries += 1

if (tries <5):
print "\nCongratulations! You guessed my number in", tries, "tries"
else:
print "\nSorry, you took too many tries to guess my number!"


raw_input("\n\n Press any key to exit..")

## Maybe now I can work on a useful project
 
N

Neil Cerutti

Thank you! It's 100% functional now, here's the final project:

title = "Guess my number game:"
print title.title()
raw_input("Press any key to continue..")

import random

number = random.randrange(99) + 1
tries = 0
guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :"))

while (guess != number) and (tries <5):
[...]

if (tries <5):
print "\nCongratulations! You guessed my number in", tries, "tries"
else:
print "\nSorry, you took too many tries to guess my number!"
raw_input("\n\n Press any key to exit..")

## Maybe now I can work on a useful project

Not quite yet. Players who guess correctly on the fifth try don't
get credit.
 
J

John Gordon

In said:
Not quite yet. Players who guess correctly on the fifth try don't
get credit.

Are you sure? tries is initialized to zero and isn't incremented for the
initial guess.
 
N

Neil Cerutti

Are you sure? tries is initialized to zero and isn't
incremented for the initial guess.

while (number != guess) and (tries < 5):

Is the condition that concludes the game.

After the game, you are told you lost if tries is not less than
five, regardless of if number == guess.
 
M

Mark Lawrence

while (number != guess) and (tries < 5):

Is the condition that concludes the game.

After the game, you are told you lost if tries is not less than
five, regardless of if number == guess.

One of these days I'll work out why some people insist on using
superfluous parentheses in Python code. Could it be that they enjoy
exercising their fingers by reaching for the shift key in conjunction
with the 9 or 0 key?
 
N

Neil Cerutti

One of these days I'll work out why some people insist on using
superfluous parentheses in Python code. Could it be that they
enjoy exercising their fingers by reaching for the shift key in
conjunction with the 9 or 0 key?

Superflous parenthesis are sometimes a nice aid to comprehension.

I don't know about the above case, though. I don't think it hurts
anything, but it doesn't add much.
 
R

Roy Smith

while (number != guess) and (tries < 5):

One of these days I'll work out why some people insist on using
superfluous parentheses in Python code. Could it be that they enjoy
exercising their fingers by reaching for the shift key in conjunction
with the 9 or 0 key?[/QUOTE]

There's lots of reasons. Some valid, some not.

Lets dispense with the invalid reason first. They've come from
C/C++/Java/whatever and are used to typing parens around the conditions
for if/for/while statements. To them, I say, "Stop trying to write
FORTRAN code in languages that aren't FORTRAN".

In this case, however, I have no problem with the extra parens. Look at
these two statements:

They have the same meaning. To correctly interpret the second one, you
need to know that != and < bind tighter than "and". One could say that
you should know that, and maybe you would be right.

On the other hand, I've long since given up trying to remember operator
precedence in various languages. If I ever have even the slightest
doubt, I just go ahead and put in the extra parens. It takes another
few ms to type, and it removes all ambiguity (for both me, and every
future person who has to read my code). And, every once in a while, it
keeps me from writing a subtle and hard-to-find bug because the
precedence rules I was sure I had remembered correctly turned out to be
wrong for the language I happened to be typing that day.

BTW, in C, I used to write:

return (foo)

for years until somebody pointed out to me that

return foo

works. I just assumed that if I had to write:

if (foo)
while (foo)
for (foo; bar; baz)

then

return (foo)

made sense too.
 
R

rusi

BTW, in C, I used to write:

return (foo)

for years until somebody pointed out to me that

return foo

works.  I just assumed that if I had to write:

if (foo)
while (foo)
for (foo; bar; baz)

then

return (foo)

made sense too.

I guess its because K&R always show their examples with
return (expr);
and so we all assume its the propah way
 
C

Chris Angelico

On the other hand, I've long since given up trying to remember operator
precedence in various languages. If I ever have even the slightest
doubt, I just go ahead and put in the extra parens.

If I ever have even the slightest doubt, I just go ahead and type
"<language> operator precedence" into a web search and check it :)
Aside from utter insanity like PHP's ternary operator being wrongly
associative, that covers pretty much everything.

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top