[Tutor] exceptions problem

Roelof Wobben rwobben at hotmail.com
Sat Sep 11 18:12:15 CEST 2010




----------------------------------------
> Date: Sat, 11 Sep 2010 11:05:54 -0400
> From: bgailer at gmail.com
> To: tutor at python.org
> Subject: Re: [Tutor] exceptions problem
>
> 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
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Hello Bob, 
 
Oke, I try to  fish.
 
When I do 
 
x= "a"
y= int(x) 
Then I get ValueError.
 
When I do 
 
x= 1.2
y=int(x)
No exception is raised.
But when I do then x ==y I get a false.
When I now do float(x) - int(x) I get 1.2 - 1 = 0.2 and that's greater then 0 
 
Because one of the two is true the Raise is executed.
 
 
x = -9 
y=int(x) 
No exception is raised.
X == y is True.
But float(x) - int(x) I get 0.0 and 0.0> 0 is False.
Because x == y is True the Raise is executed.
 
Are these the right conclusions ??
 
Roelof
 
 
  		 	   		  


More information about the Tutor mailing list