[Tutor] exceptions problem

Roelof Wobben rwobben at hotmail.com
Fri Sep 10 19:58:36 CEST 2010


 


From: rwobben at hotmail.com
To: tutor at python.org
Date: Fri, 10 Sep 2010 16:12:08 +0000
Subject: Re: [Tutor] exceptions problem





 
Date: Fri, 10 Sep 2010 18:07:13 +0200
From: fal at libero.it
To: tutor at python.org
Subject: Re: [Tutor] exceptions problem

Oops, I sent this to Roelof... Ok, I must amend it anyway...
 
On 10/09/2010 17.13, Roelof Wobben wrote:
> ...
> def readposint():
>         x = raw_input("Please enter a positive integer :")
>         try:
>                 x = int(x) and x>  0
>         except:
>                 print x , "is not a positive integer.    Try again."
>                 return False
>         return True
>
> y = readposint()
> print y
> while y == False:
>         readposint()
> print "You have entered : ", y
>
> But the x>  10 is never checked.
>
> Must I use two try except now ?
Your first problem has nothing to do with exception handling.
The culprit is Line 4:
>                 x = int(x) and x>  0
I suppose that you forgot a second equal sign between x and int(x). If 
it were
>                x == int(x) and x > 0
it would have worked as expected. But this would not trigger any 
exception, if X is a number. So let's add one:
 >                if not (x == int(x) and x > 0): raise(ValueError)
 
Hope that helps,
 
> Roelof
FrancescoHello Francesco,I change it to this :def readposint(): 
    x = raw_input("Please enter a positive integer :")
    try:
       if not (x == int(x) and x < 0): raise(ValueError) 
    except:
        print x , "is not a positive integer.  Try again."
        return False
    return Truey = readposint()
print y
while y == False:
    readposint()
print "You have entered : ", yBut -9 and 2 are both true.RoelofBecause I want to understand why this happens I wrote this test programm :def readposint(): 
    x = raw_input("Please enter a positive integer :")
    print x 
    if x == int(x) :
        print "01-True"
    else:
        print "01-False"
        print int(x)
    if x > 0 :
        print "02-True"
    else:
        print "02-False"
    return y = readposint()
print y
while y == False:
    readposint()
print "You have entered : ", y But I see wierd output :When x = 3 I get this output :Please enter a positive integer :3301-False302-TrueNoneYou have entered : None
 

That's wierd. 3 is a integer so 01 must be True and 3 is bigger then 0 so that one must be false.

 

Can someone explain me why this happens ?

 

Roelof

 


 
_______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor 
_______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100910/16506211/attachment.html>


More information about the Tutor mailing list