Simple function problem

I’m writing a funtion that takes a given number and produces all the multiples of 3 or 5 below the given number, then appends those multiples to a list and then adds them together.

def solution(number):
      nums = []
      num_list = list(range(1, number+1))
      for number in num_list:
          if number % 3 == 0 or number % 5 == 0:
              nums.append(number)
              return (sum(nums))
      else:
          return sum(nums)
      print(sum(nums))

This is my code and its just giving me multiples of ten in the follwing order:

[10
20
30
40
50
60
70]

all the way to 110. Can anyone help me figure out what I’m doing wrong?

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

I don’t know exactly what you mean by:

…the num_list contains what you intended for it to

Now, if you want to sum all the multiples of 3, here’s what you’re doing wrong:

for number in num_list:
          if number % 3 == 0 or number % 5 == 0:
              nums.append(number)
              return (sum(nums))
      else:
          return sum(nums)

For one, you return too early. You need to wait for the loop to finish, then return. Also, your indentation for the else is wrong; you would’ve needed 2 more tabs, like this

def solution(number):
      nums = []
      num_list = list(range(1, number+1))
      print(num_list)
      for number in num_list:
          if number % 3 == 0 or number % 5 == 0:
              nums.append(number)
              return (sum(nums))
          else:
              return sum(nums)
      print(sum(nums))

However, the else is unnecessary. I’m curious as to what you thought you needed it for. It appears that what you want to do is sum all the numbers in nums, which contains all the multiples of 3 or 5. So why not let the for loop finish and THEN sum the contents of nums?

def solution(number):
      nums = []
      num_list = list(range(1, number+1))

      for number in num_list:
          if number % 3 == 0 or number % 5 == 0:
              nums.append(number)
      
      print(nums)
      return sum(nums)

Result:

I thought I needed the 2nd ‘else’ to add a stop condition in case I did something wrong. I’m very new at this. Your clarification was very helpful.

Something wrong like what?

like making some sort of infinite loop that crashes my computer. I’d been following Learn Python The Hard Way until a week or 2 ago and the author says that its good practice to put an escape condition into any loops.

(Sorry for the late reply…if you’re still following this thread)

Yeah, the for loop is just going through every number in your array. Since your array is not infinite in length, there is no reason to assume it won’t just go to the next line after it operates on the last number.

Your solution was close. You’re doing well.

Thank you! I’m doing my best