[Tutor] Help with simple text book example that doesn't work!!!
steve at pearwood.info
Sun Apr 4 09:20:41 CEST 2010
On Sun, 4 Apr 2010 03:40:57 pm Brian Drwecki wrote:
> Hi all... I am working from the Learning Python 3rd edition published
> by O'Reily... FYI I am trying to learn Python on my own (not for
> course credit or anything).. I am a psychologist with very limited
> programming experience.. I am anal, and this example code doesn't
> work.. I am using IDLE to do everything (ni ni ni ni ni)
> So here is the code the book give me..
> while True:
> reply = raw_input('Enter text:')
> if reply == 'stop':
> elif not reply.isdigit( ):
> print 'Bad!' * 8
> print int(reply) ** 2
> print 'Bye'
> Idle gives me this error SyntaxError: invalid syntax (it highlights
> the word print in the print 'bye' line..
Please do an exact copy and paste of the error and post it, rather than
paraphrasing the error.
In the meantime, a couple of guesses...
Are you sure you are using Python 2.6? If you are using 3.1, that would
explain the failure. In Python 3, print stopped being a statement and
became an ordinary function that requires parentheses.
In Python 2.6, one way to get that behaviour is with the special "from
__future__ import" statement:
>>> from __future__ import print_function
>>> print "Hello world"
File "<stdin>", line 1
print "Hello world"
SyntaxError: invalid syntax
>>> print("Hello world")
Alternatively, sometimes if you have an error on one line, the
interpreter doesn't see it until you get to the next, and then you get
a SyntaxError on one line past the actual error.
E.g. if you forgot to close the bracket:
print int(reply ** 2
then you would (probably) get a SyntaxError on the line with the print.
More information about the Tutor