Test for number?

Neil Cerutti horpner at yahoo.com
Tue Sep 5 14:53:32 EDT 2006


On 2006-09-05, George Sakkis <george.sakkis at gmail.com> wrote:
> 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).

Thanks. I infer from this that in the case that int(x) raises
ValueError, that x is guaranteed to be unmodified.

-- 
Neil Cerutti
22 members were present at the church meeting held at the home
of Mrs. Marsha Crutchfield last evening. Mrs. Crutchfield and
Mrs. Rankin sang a duet, The Lord Knows Why. --Church Bulletin Blooper



More information about the Python-list mailing list