[mdlug] Python Code Question

Gregory Czerniak gregczrk at gmail.com
Fri Sep 4 23:43:17 EDT 2015


Two things:
1) You can python3-ize the above code by treating print as a function (wrap
the thing to be printed in parentheses) and using input() rather than
raw_input().
2) Interactive mode only executes one command at a time.  The "while"
statement (and all its indented statements) are one command, and print
'Bye' is another.  The interactive Python interpreter is getting confused
by that final print statement since it's not on the same indent level as
the other statements in the "while" and it's assuming everything is still
related to the "while."

On Fri, Sep 4, 2015 at 10:09 PM, JeremyBekka C <jrchristophel at gmail.com>
wrote:

> Well, I just figured out what was wrong when I ran the code in Idle. I was
> using the interpreter for Python3 not 2.7. When I used the right
> interpreter it worked fine. However, I can not figure out what is wong in
> the terminal.
>
> On Fri, Sep 4, 2015 at 9:56 PM, JeremyBekka C <jrchristophel at gmail.com>
> wrote:
>
> > Hello,
> >
> > I am trying to learn Python by working through the book "Learning Python
> > (3th Edition) - Ascher, Lutz (O'Reilly, 2008)". I have a question
> regarding
> > one of the examples in the book which is using python 2.2. The code is
> the
> > following:
> >
> > while True:
> >     reply = raw_input('Enter text:')
> >     if reply == 'stop' : break
> >     print int(reply) ** 2
> > print 'Bye
> >
> > When I run this in the Idle interpreter it gives me an error on line 4
> > with the int() built-in and says "SyntaxError: invalid syntax".
> >
> > If I run this in the terminal I get the following:
> >
> > >>> while True:
> > ...       reply = raw_input('Enter text:')
> > ...       if reply == 'stop' : break
> > ...       print int(reply) ** 2
> > ...    print 'Bye'
> >   File "<stdin>", line 5
> >     print 'Bye'
> >         ^
> > SyntaxError: invalid syntax
> >
> > Any idea what is wrong here? Iooked up the integer built-in on the python
> > website and it did not give me any ideas what is wrong.
> >
> > Thanks
> >
> _______________________________________________
> mdlug mailing list
> mdlug at mdlug.org
> http://mdlug.org/mailman/listinfo/mdlug
>


More information about the mdlug mailing list