[Tutor] FW: null inputs
ADRIAN KELLY
kellyadrian at hotmail.com
Thu Mar 15 16:08:56 CET 2012
thanks very much i get it now...............
> From: hugo.yoshi at gmail.com
> Date: Thu, 15 Mar 2012 16:03:12 +0100
> Subject: Re: [Tutor] FW: null inputs
> To: eire1130 at gmail.com
> CC: kellyadrian at hotmail.com; tutor at python.org
>
> On Thu, Mar 15, 2012 at 3:56 PM, James Reynolds <eire1130 at gmail.com> wrote:
> >
> >
> > You can't prevent users from entering whatever they feel like it, but you
> > can prevent your program from processing that input and force them to try
> > again.
> >
> > The typical way this is done is through a while loop:
> >
> > age = ''
> >
> > while age != ''
> > age=raw_input("what age are you? ")
> >
> > this will continue to loop until you give it something other than ''.
> >
> > Of course, you can build on this, you know, something like:
> >
> > checker = False
> >
> > while checker:
> > age=raw_input("what age are you? ")
> > try:
> > age = int(age)
> > checker = True
> > except TypeError:
> > print "you didn't enter an integer for an age!"
> > print "try again!!!
> >
> >
>
> Small correction: the exception you're looking for in this case is a
> ValueError (after all, the argument provided has the correct type, but
> its value can not be converted to an integer). int will throw
> TypeError, but only if you supply an argument that is not a string or
> number.
>
> Hugo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120315/b951bbd9/attachment-0001.html>
More information about the Tutor
mailing list