[mdlug] Python Code Question

JeremyBekka C jrchristophel at gmail.com
Sat Sep 5 20:46:23 EDT 2015


Okay, that makes sense. Thanks for you help!

On Sat, Sep 5, 2015 at 8:36 PM, Gregory Czerniak <gregczrk at gmail.com> wrote:

> If you really like the interpreter, an alternative strategy would be to
> wrap everything in a function first (function definitions are single
> commands) and then run that function.  Here's an example:
>
> >>> def main():
> ...   while True:
> ...     reply = raw_input('Enter text:')
> ...     if reply == 'stop': break
> ...     print int(reply) ** 2
> ...   print 'Bye'
> ...
> >>> main()
> Enter text:2
> 4
> Enter text:5
> 25
> Enter text:stop
> Bye
> >>>
>
> On Sat, Sep 5, 2015 at 4:04 PM, JeremyBekka C <jrchristophel at gmail.com>
> wrote:
>
> > Thanks for your help! It makes sense now when I realise that the
> > interpreter cannot run more that one command at a time. When I indent the
> > print 'Bye' command under the previous command it works fine. So, the
> only
> > way to be able to run that code in interactive mode is to write it as a
> > script and then import it into the interpreter?
> >
> > Thanks!
> >
> > On Fri, Sep 4, 2015 at 11:43 PM, Gregory Czerniak <gregczrk at gmail.com>
> > wrote:
> >
> > > 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
> > > >
> > > _______________________________________________
> > > mdlug mailing list
> > > mdlug at mdlug.org
> > > http://mdlug.org/mailman/listinfo/mdlug
> > >
> > _______________________________________________
> > mdlug mailing list
> > mdlug at mdlug.org
> > http://mdlug.org/mailman/listinfo/mdlug
> >
> _______________________________________________
> mdlug mailing list
> mdlug at mdlug.org
> http://mdlug.org/mailman/listinfo/mdlug
>


More information about the mdlug mailing list