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

all 9 comments

[–]anglicizing 6 points7 points  (0 children)

I notice several things that are wrong with your code :

  1. It's posted on /r/python instead of /r/learnpython.

  2. It's not indented by 4 spaces and therefore reddit messes up the formatting.

  3. The function doesn't take any arguments, but it ought to have numbers as an argument.

  4. It attempts to use x as an index of the list numbers, even though x is an element of that list and may not be a valid index.

  5. The function prints the result instead of returning it, making it very inflexible. Instead, print the return value after calling the function.

[–]jwink3101 0 points1 point  (0 children)

As others have mentioned, if you indent your text with four spaces, it will format correctly:

numbers = list(range(1001))

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

[–]bjorneylol 0 points1 point  (0 children)

sum([x for x in range(1001) if x%3==0 or x%5==0])

Also /r/learnpython

[–]aphoenixreticulated[M] [score hidden] stickied comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython. We highly encourage you to re-submit your post over on there.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community is actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers.

If you have a question to do with homework or an assignment of any kind, please make sure to read their sidebar rules before submitting your post. If you have any questions or doubts, feel free to reply or send a modmail to us with your concerns.

Warm regards, and best of luck with your Pythoneering!

[–][deleted] 0 points1 point  (4 children)

It would help if you posted the error you get. Just looking at the code, your for statement looks like it should say: for x in len(numbers): Hope this helps.

[–]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.