symple programming task
Tim Chase
python.list at tim.thechases.com
Mon Apr 21 09:39:10 EDT 2014
On 2014-04-21 06:21, Ivan Ivanivich wrote:
> > Find the sum of all the multiples of 3 or 5 below 1000.
> my new version of script:
>
> total = 0
> div1 = 3
> div2 = 5
> for basis in range(0, 1000):
> mod = basis % div1
> if mod == 0:
> total = total + basis
> continue
> mod = basis % div2
> if mod == 0:
> total = total + basis
> continue
>
>
>
> print("total = ", total)
Now that you have a working solution, I don't mind giving my more
pythonic solution:
sum(dividend for dividend in range(1000)
if any(dividend % divisor == 0 for divisor in (3, 5)))
which succinctly states the problem and makes it easy to
add/remove/change the divisors in one place rather than having to
define multiple variables to hold them and "if" statements to
evaluate them.
-tkc
More information about the Python-list
mailing list