[Tutor] Help with simple text book example that doesn't work!!!

Brian Drwecki drwecki at gmail.com
Sun Apr 4 15:15:20 CEST 2010


Python version


Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)]
on win32
Type "copyright", "credits" or "license()" for more information


As for the exact error code....here it is.

>>>
while True:
    reply = raw_input('Enter text:')
    if reply == 'stop':
        break
    elif not reply.isdigit(  ):
        print 'Bad!' * 8
    else:
        print int(reply) ** 2
print 'bye'
SyntaxError: invalid syntax  (note it highlights the print code)...

This works!!!
>>> print'bye'
bye
>>>



I tried adding parantheses around the print in the code and get the same
error


It only says SyntaxError:invalid syntax and it highlights the print word..



According to the book, this print should work, but it doesn't?   Any
thoughts? Does it work on your computer?



On Sun, Apr 4, 2010 at 2:20 AM, Steven D'Aprano <steve at pearwood.info> wrote:

> 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':
> >         break
> >     elif not reply.isdigit(  ):
> >         print 'Bad!' * 8
> >     else:
> >         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")
> 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:
>
> ...
> else:
>    print int(reply ** 2
> print 'Bye'
>
> then you would (probably) get a SyntaxError on the line with the print.
>
>
>
> --
> Steven D'Aprano
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100404/f8732525/attachment.html>


More information about the Tutor mailing list