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.

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

Edited 3 Years Ago by james.lu.75491856

This question has already been answered. Start a new discussion instead.