Test for number?
Neil Cerutti
horpner at yahoo.com
Tue Sep 5 10:29:20 EDT 2006
On 2006-09-04, George Sakkis <george.sakkis at gmail.com> wrote:
> Dr. Pastor wrote:
>> In the following code I would like to ascertain that x has/is
>> a number. What the simplest TEST should be? (Could not find
>> good example yet.)
>> ---
>> x=raw_input('\nType a number from 1 to 20')
>> if TEST :
>> Do_A
>> else:
>> Do_B
>> ---
>> Thanks for any guidance.
>
> 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?
--
Neil Cerutti
More information about the Python-list
mailing list