Re: [Tutor] Most pythonic input validation

2009-10-16 Thread Dave Angel
Kent Johnson wrote: Is this the way to define my own error and to use it: class MyValueError(Exception): define initialization and printing You usually don't need to define anything extra, the Exception methods are fine. I think there could be confusion over what you meant w

Re: [Tutor] Most pythonic input validation

2009-10-16 Thread Kent Johnson
On Fri, Oct 16, 2009 at 9:44 AM, Robert Johansson wrote: > If I wanted prompt to be an integer, is my check with range still bad? If the prompt is coming from raw_input() then you have already guaranteed it is an integer when you convert it in a try/except block. > Is this the way to define my o

Re: [Tutor] Most pythonic input validation

2009-10-16 Thread Robert Johansson
> What if a valid user input has to be an integer between 10 and 20? I mean, > it could be that you are doing something that only makes sense for that > input. Would it be pythonic to add a test like: > > If prompt in range(10,21): Better to write if 10 <= prompt <= 20: >  ... > else: >  ... ra

Re: [Tutor] Most pythonic input validation

2009-10-16 Thread Alan Gauld
"Robert Johansson" wrote What if a valid user input has to be an integer between 10 and 20? Would it be pythonic to add a test like: If prompt in range(10,21): ... else: ... raise an error Yes and it could be a ValueError in this case. You could also test using conditions which, for a w

Re: [Tutor] Most pythonic input validation

2009-10-16 Thread Christian Witts
Robert Johansson wrote: 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

Re: [Tutor] Most pythonic input validation

2009-10-16 Thread Kent Johnson
On Fri, Oct 16, 2009 at 7:25 AM, Robert Johansson wrote: > What if a valid user input has to be an integer between 10 and 20? I mean, > it could be that you are doing something that only makes sense for that > input. Would it be pythonic to add a test like: > > If prompt in range(10,21): Better

Re: [Tutor] Most pythonic input validation

2009-10-16 Thread Robert Johansson
> 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: >         

Re: [Tutor] Most pythonic input validation

2009-10-16 Thread Christian Witts
Katt wrote: Message: 3 Date: Thu, 15 Oct 2009 16:50:57 +0100 From: Rich Lovely To: Wayne Werner Cc: "tutor@python.org" Subject: Re: [Tutor] Most pythonic input validation Message-ID: Content-Type: text/plain; charset=windows-1252 2009/10/15 Wayne Werner : Hi, I'm writi

Re: [Tutor] Most pythonic input validation

2009-10-16 Thread Katt
Message: 3 Date: Thu, 15 Oct 2009 16:50:57 +0100 From: Rich Lovely To: Wayne Werner Cc: "tutor@python.org" Subject: Re: [Tutor] Most pythonic input validation Message-ID: Content-Type: text/plain; charset=windows-1252 2009/10/15 Wayne Werner : Hi, I'm writing a text based m

Re: [Tutor] Most pythonic input validation

2009-10-15 Thread Emile van Sebille
On 10/15/2009 7:36 AM Wayne Werner said... Is that the most pythonic way of validating? Is there a better way? Pythonic in part having been addressed, I've some further comments to your valid_choice code... -- Emile def valid_choice(choice, min, max): # don't use min and max -- they shadow

Re: [Tutor] Most pythonic input validation

2009-10-15 Thread Rich Lovely
2009/10/15 Wayne Werner : > > > On Thu, Oct 15, 2009 at 10:50 AM, Rich Lovely > wrote: >> >> 2009/10/15 Wayne Werner : >> > 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 >> > prope

Re: [Tutor] Most pythonic input validation

2009-10-15 Thread Wayne Werner
On Thu, Oct 15, 2009 at 10:50 AM, Rich Lovely wrote: > 2009/10/15 Wayne Werner : > > 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:

Re: [Tutor] Most pythonic input validation

2009-10-15 Thread Rich Lovely
2009/10/15 Wayne Werner : > 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: >

[Tutor] Most pythonic input validation

2009-10-15 Thread Wayne Werner
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_