<br><br><div class="gmail_quote">On Mon, Jan 12, 2009 at 5:23 PM, WM. <span dir="ltr">&lt;<a href="mailto:wferguson1@socal.rr.com" target="_blank">wferguson1@socal.rr.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

# The natural numbers(natnum), under 1000, divisible by 3 or by 5 are to be added together.<br>
natnum = 0<br>
num3 = 0<br>
num5 = 0<br>
cume = 0<br>
# The &#39;and&#39; is the 15 filter; the &#39;or&#39; is the 3 or 5 filter.<br>
while natnum &lt;= 999:<br>
 &nbsp; &nbsp;num3 = natnum/3<br>
 &nbsp; &nbsp;num5 = natnum/5<br>
 &nbsp; &nbsp;if natnum - (num3 * 3) == 0 and natnum - (num5 * 5) == 0:<br>
 &nbsp; &nbsp; &nbsp; &nbsp;cume = cume + natnum<br>
 &nbsp; &nbsp;elif natnum - (num3 * 3) == 0 or natnum - (num5 * 5) == 0:<br>
 &nbsp; &nbsp; &nbsp; &nbsp;if natnum - (num3 * 3) == 0:<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cume = cume + natnum<br>
 &nbsp; &nbsp; &nbsp; &nbsp;elif natnum - (num5 * 5) == 0:<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cume = cume + natnum<br>
 &nbsp; &nbsp;natnum = natnum + 1<br>
print cume<br>
<br>
<br>
This problem was kicked around last month and I did not understand any of the scripts. &nbsp;So I tried to recall the BASIC ifs and loops. The project euler guys say it works, but how might it be made more Pythonic?<br>
_______________________________________________<br>
Tutor maillist &nbsp;- &nbsp;<a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</blockquote></div><br><br clear="all">I guess using a list comprehension will work<br>&gt;&gt;&gt;x = [number for number in range(1000) if number%3 ==0 or number%2 ==0]<br>&gt;&gt;&gt; sum(x)<br>333167<br>&gt;&gt;&gt; <br>

<br>-- <br>لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد الغزالي<br>&quot;No victim has ever been more repressed and alienated than the truth&quot;<br><br>Emad Soliman Nawfal<br>Indiana University, Bloomington<br>

<a href="http://emnawfal.googlepages.com" target="_blank">http://emnawfal.googlepages.com</a><br>--------------------------------------------------------<br>