[Tutor] exceptions problem
bob gailer
bgailer at gmail.com
Sat Sep 11 17:05:54 CEST 2010
On 9/11/2010 6:56 AM, Peter Otten wrote:
> Steven D'Aprano wrote:
>
>> On Sat, 11 Sep 2010 09:56:41 am bob gailer wrote:
>>>> I never thought that you can use a float and a integer to look if
>>>> the number is a integer.
>>> You can't.
>>
I made that comment in the context of the OPs function:
def readposint():
x = raw_input("Please enter a positive integer :")
try:
if (int(x)<0 or (float(x) - int(x) > 0)): raise(ValueError)
except:
print x , "is not a positive integer. Try again."
return -1
return x
The OP thought (incorrectly) that, given for example:
x = '3.1'
float(x) - int(x) would evaluate to 0.1
In reality int(x) in this case raises an exception.
ValueError: invalid literal for int() with base 10: '3.1'
Since the expression was in a try he could not tell exactly what was
happening.
I also don't quite understand the use of raise in the try.
I wish and hope that Roelof will learn how to do program walkthroughs
and use the interactive prompt to solve things himself. I applaud the
patience some of you have ih hand-holding him. I don't have that
patience. I wish him to learn to fish.
--
Bob Gailer
919-636-4239
Chapel Hill NC
More information about the Tutor
mailing list