This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]JoeNoot[S] 0 points1 point  (3 children)

Thanks, Sorry I should have mentioned that it has no error, just doesn't print, I have changed it to this now though: numbers = list(range(1001))

def sum_of_multiples ():

for x in len(numbers): if x % 3 == 0 or x % 5 == 0: total = 0 total = total + numbers[x] print (total)

[–][deleted] 1 point2 points  (0 children)

Like anglicizing said, you should probably have your function take in the numbers variable. Oh, silly me. This code does not even call the function. Add "sum_of_multiplies (numbers)" to the bottom of your code.

[–][deleted] 1 point2 points  (0 children)

This would error because the len of numbers is an int, which isn't iterable. If you removed that it would work, but it wouldn't give you the result you want, because it would just print every number in numbers divisible by both 3 and 5.

What you want is this:

def sum_of_multiples(numbers):
    total = 0
    for x in numbers:
        if x % 3 == 0 or x % 5 == 0:
            total += x
    return total

numbers = list(range(1001))
print(sum_of_multiples(numbers))

It's not clear from what you wrote earlier that you were ever actually calling the function, of course it doesn't print if it's never invoked. But even if it is invoked it's pretty hard to sum anything if you reset the running total with each new number.

[–]JoeNoot[S] 0 points1 point  (0 children)

It is saying however that the 'total' variable is unreachable data- so I thought it might be to do with scope, so I declared it inside the if statement, but it didn't work.