[Tutor] Most pythonic input validation

Katt the_only_katala at verizon.net
Fri Oct 16 10:37:25 CEST 2009

> Message: 3
> Date: Thu, 15 Oct 2009 16:50:57 +0100
> From: Rich Lovely <roadierich at googlemail.com>
> To: Wayne Werner <waynejwerner at gmail.com>
> Cc: "tutor at python.org" <Tutor at python.org>
> Subject: Re: [Tutor] Most pythonic input validation
> Message-ID:
> <f0b4202b0910150850w7fe680c0v400bc5d432f716b at mail.gmail.com>
> Content-Type: text/plain; charset=windows-1252
> 2009/10/15 Wayne Werner <waynejwerner at gmail.com>:
>> Hi,
>> I'm writing a text based menu and want to validate the user input. I'm
>> giving the options as integers, and I want to make sure the user enters a
>> proper value.
>> Here's what I've got so far:?http://pastebin.com/m1fdd5863
>> I'm most interested in this segment:
>> ?? ?while True:
>> ?? ? ? ?choice = raw_input(prompt)
>> ?? ? ? ?if valid_choice(choice, 0, len(options)-1):
>> ?? ? ? ? ? ?break
>> ?? ?return choice
>> Is that the most pythonic way of validating? Is there a better way?
>> As an aside, in the valid_choice function I know I could do:
>> if not choice in range(min, max)
>> but I figured a comparison would probably be the better choice, correct?
>> Thanks,
>> Wayne
>> --
>> To be considered stupid and to be told so is more painful than being 
>> called
>> gluttonous, mendacious, violent, lascivious, lazy, cowardly: every 
>> weakness,
>> every vice, has found its defenders, its rhetoric, its ennoblement and
>> exaltation, but stupidity hasn?t. - Primo Levi
>> _______________________________________________
>> Tutor maillist ?- ?Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
> The most pythonic way would be to use a try except block:
>    while True:
>        choice = raw_input(prompt)
>        try:
>            options[int(choice)]
>        except (KeyError, IndexError, TypeError):
>            print "Invalid input, try again."
>            continue
>        return choice
> Also, did you want to convert choice to an int at some point?  You
> appear to be comparing it to a number in valid_choice(), but it will
> be passed out of the method as a str.
> Hence I've added a conversion to int, and I'm catching KeyError (for
> dicts), IndexError (for lists), and TypeError, for when int(choice)
> fails.
> This is a principle called "It's Easier to Ask Forgiveness than
> Permission" (EAFP), which is one of the pythonic principles.
> Hope that helps.
> -- 
> Rich "Roadie Rich" Lovely

As I am a new programmer I am specifically trying to pay attention to when 
you guys discuss making thing more python like.

I am a bit confused though on a couple things in this code.  Probably since 
I have not dealt with it as of yet.

The first is the while statement.  I have made a successful menu selection 
but my while statement is:
    while prompt != 0:
0 of course meaning exit.  Does "while True:" mean the same?

Second is the input statement.  Could you not just put choice = 
int(raw_input(prompt)) instead of using two different statements?  Or is it 
that my example will return as string?

The third is the "try" statement.  How is this different from the "for" loop 
or "if" loop?

The fourth is except (KeyError, IndexError, TypeError):  why do you check 
for three different types of errors?

Thanks in advance,


More information about the Tutor mailing list