<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'>
<BR> <BR>Date: Fri, 10 Sep 2010 18:07:13 +0200<BR>From: fal@libero.it<BR>To: tutor@python.org<BR>Subject: Re: [Tutor] exceptions problem<BR><BR><PRE>Oops, I sent this to Roelof... Ok, I must amend it anyway...<BR> <BR>On 10/09/2010 17.13, Roelof Wobben wrote:<BR>> ...<BR>> def readposint():<BR>> x = raw_input("Please enter a positive integer :")<BR>> try:<BR>> x = int(x) and x> 0<BR>> except:<BR>> print x , "is not a positive integer. Try again."<BR>> return False<BR>> return True<BR>><BR>> y = readposint()<BR>> print y<BR>> while y == False:<BR>> readposint()<BR>> print "You have entered : ", y<BR>><BR>> But the x> 10 is never checked.<BR>><BR>> Must I use two try except now ?<BR>Your first problem has nothing to do with exception handling.<BR>The culprit is Line 4:<BR>> x = int(x) and x> 0<BR>I suppose that you forgot a second equal sign between x and int(x). If <BR>it were<BR>> x == int(x) and x > 0<BR>it would have worked as expected. But this would not trigger any <BR>exception, if X is a number. So let's add one:<BR> > if not (x == int(x) and x > 0): raise(ValueError)<BR> <BR>Hope that helps,<BR> <BR>> Roelof<BR>Francesco</PRE><PRE>Hello Francesco,</PRE><PRE>I change it to this :</PRE><PRE>def readposint(): <BR> x = raw_input("Please enter a positive integer :")<BR> try:<BR> if not (x == int(x) and x < 0): raise(ValueError) <BR> except:<BR> print x , "is not a positive integer. Try again."<BR> return False<BR> return True</PRE><PRE>y = readposint()<BR>print y<BR>while y == False:<BR> readposint()<BR>print "You have entered : ", y</PRE><PRE>But -9 and 2 are both true.</PRE><PRE>Roelof</PRE><PRE><BR> </PRE><BR>_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor                                            </body>
</html>