[Tutor] exceptions problem

Francesco Loffredo fal at libero.it
Fri Sep 10 18:07:13 CEST 2010


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
Francesco
-------------- next part --------------

Nessun virus nel messaggio in uscita.
Controllato da AVG - www.avg.com 
Versione: 9.0.851 / Database dei virus: 271.1.1/3124 -  Data di rilascio: 09/09/10 08:34:00


More information about the Tutor mailing list