<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'>
<BR>&nbsp;<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>&gt; ...<BR>&gt; def readposint():<BR>&gt;         x = raw_input("Please enter a positive integer :")<BR>&gt;         try:<BR>&gt;                 x = int(x) and x&gt;  0<BR>&gt;         except:<BR>&gt;                 print x , "is not a positive integer.    Try again."<BR>&gt;                 return False<BR>&gt;         return True<BR>&gt;<BR>&gt; y = readposint()<BR>&gt; print y<BR>&gt; while y == False:<BR>&gt;         readposint()<BR>&gt; print "You have entered : ", y<BR>&gt;<BR>&gt; But the x&gt;  10 is never checked.<BR>&gt;<BR>&gt; 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>&gt;                 x = int(x) and x&gt;  0<BR>I suppose that you forgot a second equal sign between x and int(x). If <BR>it were<BR>&gt;                x == int(x) and x &gt; 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> &gt;                if not (x == int(x) and x &gt; 0): raise(ValueError)<BR> <BR>Hope that helps,<BR> <BR>&gt; Roelof<BR>Francesco</PRE><PRE>Hello Francesco,</PRE><PRE>I change it to this :</PRE><PRE>def readposint(): <BR>&nbsp;&nbsp;&nbsp; x = raw_input("Please enter a positive integer :")<BR>&nbsp;&nbsp;&nbsp; try:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if not (x == int(x) and x &lt; 0): raise(ValueError) <BR>&nbsp;&nbsp;&nbsp; except:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print x , "is not a positive integer.&nbsp; Try again."<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return False<BR>&nbsp;&nbsp;&nbsp; return True</PRE><PRE>y = readposint()<BR>print y<BR>while y == False:<BR>&nbsp;&nbsp;&nbsp; readposint()<BR>print "You have entered : ", y</PRE><PRE>But -9 and 2 are both true.</PRE><PRE>Roelof</PRE><PRE><BR>&nbsp;</PRE><BR>_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor                                               </body>
</html>