[Tutor] exceptions problem
Roelof Wobben
rwobben at hotmail.com
Fri Sep 10 18:12:08 CEST 2010
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.Roelof
_______________________________________________ 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/4c6c5121/attachment.html>
More information about the Tutor
mailing list