<span class="Apple-style-span" style="font-family: Helvetica; font-size: medium; ">Isn't one of the unsolved millenium prize problems one that includes the ability to find all of the prime numbers? I'm not sure if your program is possible if the input number is large.</span><div style="font-family: Helvetica; font-size: medium; ">
<br></div><div style="font-family: Helvetica; font-size: medium; ">But to check if a number x is an int, just do this:</div><div style="font-family: Helvetica; font-size: medium; "><br></div><div style="font-family: Helvetica; font-size: medium; ">
x == int(x)</div><div style="font-family: Helvetica; font-size: medium; "><br></div><div style="font-family: Helvetica; font-size: medium; "><br></div><div style="font-family: Helvetica; font-size: medium; ">Rachel</div><div style="font-family: Helvetica; font-size: medium; ">
<br></div><br><div class="gmail_quote">On Tue, May 31, 2011 at 2:38 PM, Hugo Arts <span dir="ltr"><<a href="mailto:hugo.yoshi@gmail.com">hugo.yoshi@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On Tue, May 31, 2011 at 11:30 PM, Joel Goldstick<br>
<<a href="mailto:joel.goldstick@gmail.com">joel.goldstick@gmail.com</a>> wrote:<br>
><br>
><br>
> <a href="http://stackoverflow.com/questions/1265665/python-check-if-a-string-represents-an-int-without-using-try-except" target="_blank">http://stackoverflow.com/questions/1265665/python-check-if-a-string-represents-an-int-without-using-try-except</a><br>
><br>
> def RepresentsInt(s):<br>
><br>
> try:<br>
> int(s)<br>
><br>
> return True<br>
> except ValueError:<br>
><br>
> return False<br>
><br>
>>>> print RepresentsInt("+123")<br>
><br>
> True<br>
>>>> print RepresentsInt("10.0")<br>
><br>
> False<br>
><br>
<br>
</div>For strings, that works, but not for integers:<br>
<br>
>>> int(10.0)<br>
10<br>
<br>
And if you want to check if one number is divisible by another, you're<br>
not going to call it on strings.<br>
<br>
A better way is to use the modulo operator, which gives the remainder<br>
when dividing:<br>
<br>
>>> a = 6<br>
>>> a % 3<br>
0<br>
>>> a % 4<br>
2<br>
<br>
So, if the remainder is zero the left operand is divisible by the right one.<br>
<font color="#888888"><br>
Hugo<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</div></div></blockquote></div><br>