Date: Fri, 10 Sep 2010 18:07:13 +0200 From: f...@libero.it To: tutor@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@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor