Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   First python program, syntax error in while loop (http://www.velocityreviews.com/forums/t960337-first-python-program-syntax-error-in-while-loop.html)

ryankoch38@gmail.com 05-03-2013 05:18 PM

First python program, syntax error in while loop
 
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

Chris “Kwpolska” Warrick 05-03-2013 05:26 PM

Re: First python program, syntax error in while loop
 
On Fri, May 3, 2013 at 7:18 PM, <ryankoch38@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 :")
>
> 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
> --
> http://mail.python.org/mailman/listinfo/python-list


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.

--
Kwpolska <http://kwpolska.tk> | GPG KEY: 5EAAEA16
stop html mail | always bottom-post
http://asciiribbon.org | http://caliburn.nl/topposting.html

John Gordon 05-03-2013 05:27 PM

Re: First python program, syntax error in while loop
 
In <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> ryankoch38@gmail.com writes:

> 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.

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"


MRAB 05-03-2013 05:36 PM

Re: First python program, syntax error in while loop
 
On 03/05/2013 18:18, ryankoch38@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 :")
>
> 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..")



Zachary Ware 05-03-2013 05:37 PM

Re: First python program, syntax error in while loop
 
On Fri, May 3, 2013 at 12:18 PM, <ryankoch38@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 ;).

John Gordon 05-03-2013 05:37 PM

Re: First python program, syntax error in while loop
 
In <mailman.1272.1367602030.3114.python-list@python.org> =?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?= <kwpolska@gmail.com> writes:

> 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

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"


ryankoch38@gmail.com 05-03-2013 05:57 PM

Re: First python program, syntax error in while loop
 
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

John Gordon 05-03-2013 06:21 PM

Re: First python program, syntax error in while loop
 
In <02b65e11-89c6-4639-9d93-27e1f90eed66@googlegroups.com> ryankoch38@gmail.com writes:

> 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".

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"


ryankoch38@gmail.com 05-03-2013 06:34 PM

Re: First python program, syntax error in while loop
 
Oh wow I can't believed I derped that hard!!!!
Thanks Lol.

ryankoch38@gmail.com 05-03-2013 07:52 PM

Re: First python program, syntax error in while loop
 
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


All times are GMT. The time now is 11:53 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.