[Tutor] pausing and layout

Gonçalo Rodrigues op73418@mail.telepac.pt
Sat Jan 4 13:10:02 2003


----- Original Message -----
From: "mike O" <skitzomonkey@hotmail.com>
To: <Janssen@rz.uni-frankfurt.de>
Cc: <tutor@python.org>
Sent: Saturday, January 04, 2003 5:53 PM
Subject: Re: [Tutor] pausing and layout


>
>
>
>
>
>
>
> >From: Michael Janssen <Janssen@rz.uni-frankfurt.de>
> >To: mike O <skitzomonkey@hotmail.com>
> >CC: <tutor@python.org>
> >Subject: Re: [Tutor] pausing and layout
> >Date: Sat, 4 Jan 2003 18:47:12 +0100 (CET)
> >
> >On Sat, 4 Jan 2003, mike O wrote:
> >
> > > I guess I have three questions, the first being: Is there a python
> >command
> > > to pause until the user does something?
> >Hello Mike
> >
> >In addition to Gonçalo's reply I want to say that raw_input() is
typically
> >used in a while loop:
> >
> >from types import IntType
> >          ...
> >
> >     while 1:
> >         p = raw_input("Choose a Number or [Q]uit: ")
> >         if p == "Q" or p == "q" or p == "":
> >             # User requests the end of the game
> >             sys.exit()
> > elif type(p) == IntType:
> >             # we have checked if p is sufficient and exit while loop
> >             break
> >         else:
> >             # we've got some input, but it was the wrong: continue
> >             # with the while loop and ask the user again
> >             print "\tError: please choose a number."
> >     #NOW do something with "p"
> >
> If you just wanted a number, couldn't you use input instead of raw_input?
> I'm not entirely sure on these two functions, other than that I thought
> input was for an integer, and raw_input was for a string, is that right?
>

No. input is just a call to raw_input *and then* an evaluation of the
returned string via the eval function, that is, input is the same as

eval(raw_input("A number, please:"))

Since it does no checking whatsoever this is an absolute no-no in terms of
security - the user can do *everything* that Python is capable (yes,
including formatting the hard disk). If you really want/expect an integer
just follow the usual Python idiom: try to convert it, handling any ensuing
exceptions, e.g. something like

while 1:
    s = raw_input("A number, please:")
    try:
        number = int(s)
        break
    except ValueError:
        print "Hey buster, I *want* a number"

If you don't know while loop's or exception-handling I suggest you start at
the newbie tutorials (go to the newbies section in www.python.org) and post
any questions you might have here.

All the best,
G. Rodrigues