Test for number?

George Sakkis george.sakkis at gmail.com
Tue Sep 5 13:43:32 EDT 2006


Neil Cerutti wrote:

> On 2006-09-04, George Sakkis <george.sakkis at gmail.com> wrote:
> > x=raw_input('\nType a number from 1 to 20')
> > try:
> >     x = int(x)
> >     if x<1 or x>20: raise ValueError()
> > except ValueError:
> >     Do_B
> > else:
> >     Do_A
> >
> > If you want to distinguish between the two error cases (not a
> > number vs number not in [1,20]), handle the second one as
> > "Do_C" instead of raising ValueError.
>
> Is the original value of x available in Do_B and Do_A, or will it
> have been clobbered before getting there?

In Do_A, x will be an integer between 1 and 20. In Do_B, it depends; if
the original input cannot be converted to an int, it will be preserved,
otherwise x will be an integer (lower than 1 or larger than 20).

George




More information about the Python-list mailing list