[Tutor] Most pythonic input validation

Kent Johnson kent37 at tds.net
Fri Oct 16 14:12:50 CEST 2009

On Fri, Oct 16, 2009 at 7:25 AM, Robert Johansson
<robert.johansson at math.umu.se> 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 to write
  if 10 <= prompt <= 20:
>  ...
> else:
>  ... raise an error
> If I understand the error handling correctly you can add your own user
> defined error, but is that really what you should do in that case?

It depend on the context. You could raise ValueError (it's OK to raise
built-in exception types if they apply) or your own exception type or
just handle the error.


More information about the Tutor mailing list