[Tutor] Most pythonic input validation

Kent Johnson kent37 at tds.net
Fri Oct 16 17:54:29 CEST 2009

On Fri, Oct 16, 2009 at 9:44 AM, Robert Johansson
<robert.johansson at math.umu.se> 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 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.

> try:
>  if test ok:
>     ...
>  else:
>     raise MyValueError

raise MyValueError() # with parentheses to create an instance

> except MyValueError:


More information about the Tutor mailing list