I am going through challanges in python and cant see why i'm not getting the correct answer... The challange is 'Find the sum of all the multiples of 3 or 5 below 1000.'

Here is my code:

``````#!/usr/local/bin/python2.7

a = 0
b = 0

eA = []
eA2 = []

while (a < 997):
# for 3
a += 3
eA.append(a)

while (b < 995):
# for 5
b += 5
eA2.append(b)

print(eA + eA2)
``````

The sum of the numbers outputted is not correct and I can't see why.

You are counting twice numbers divisible by 15, I think.

## All 4 Replies

Use for loops and list comprehensions

``````a = [i for i in range(995) if i%5==0] # for every number in 0-995 if it's remainder when divided with 5 equals 0, put it in a list
b = [i for i in range(995) if i%3==0] #same
print(a+b) #wrong! outputs alist of all the numbers
print(sum(a+b))#correct
``````

You are counting twice numbers divisible by 15, I think.

Here is a hint:

``````sum3 = 0
for n in range(0, 1000, 3):
#print(n)  # test
sum3 += n

print(sum3)
``````

Ah, thamk you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.