# FAQ: Code Challenge: Loops - Divisible by Ten

my inefficient-but-it-works solve

``````def divisible_by_ten(nums):
i = 0
for num in nums:
if num % 10 == 0:
i = i + 1

return i
``````

3 posts were split to a new topic: Does modulo need to be in parenthesis?

2 posts were split to a new topic: Can this code be simplified?

2 posts were split to a new topic: Why does my code stop?

4 posts were split to a new topic: How can I overwrite numbers in a list?

2 posts were split to a new topic: Where is the bug in my code?

More elegant than mine!

Hi guys,
I’m sure i’m going about it it the wrong way but if anyone can give me insights into what’s happening with the code, i’d greatly appreciate it.

From my understanding, my code (at the bottom) should iterate through the entire list however it seems to stop after the first one, can anyone advise as to why (or if it’s doing anything else)?

``````def divisible_by_ten(nums):
count = 0
for number in nums:
if (number % 10 == 0):
count += 1
return count

def divisible_by_ten(num):
count1 = len(num)
print(count1)
for numbers in list(range((count1))):
print(list(range(count1)))
print(num)
if num[numbers]%10!=0:
continue
new_list =[ ]
new_list.append(num[numbers])
print(new_list)
return len(new_list)
``````

I recommend running your code here:

http://www.pythontutor.com/visualize.html#mode=edit

to help you to identify the issue. This allows you to step through your code. You might need to add a function call.

1 Like

thank you! I’ve used it a fair bit

Still have question left? Or is everything crystal clear now?

``````#Write your function here
def divisible_by_ten(nums):
list = []
for num in nums:
if num % 10 == 0:
list.append(num)
return len(list)
#Uncomment the line below when your function is done
print(divisible_by_ten([20, 25, 30, 35, 40]))
``````

Could someone explain to me if this is an advisable solution to this code challenge

That depends, if you are only interested in how many numbers are divisible by 10, and not which numbers that are, i wouldn’t use a list

i would simply use:

``````count = 0
``````

then increase count every time your if condition is true

Hello

I dont understand why im only getting 1 in the count as oppossed to 3.

can someone please give me a hand

here is the code

The assignment operators always place the operation ahead of the assignment.

``````+=
-=
*=
/=
//=
%=
**=
``````

and so on.

Thanks!

Ok, so I changed that part of the code, but im still getting 1 as opposed to 3

why is it only runing the loop once?

again, here is the code

Should `return` be inside the loop block?

1 Like

Hi everyone! so i wrote this code:

``````y = []

def divisible_by_ten(nums):

for num in nums:

if num % 10 == 0:

y.append(num)

return len(y)
``````

and it does return 3. However, it says there is a mistake:
" `divisible_by_ten([20, 25, 30, 35, 40])` should have returned `3` , and it returned 6"

Any idea how to fix it? Thanks a lot!

1 Like

well, if we call the function multiple times:

``````divisible_by_ten([20, 25, 30, 35, 40])
divisible_by_ten([20, 25, 30, 35, 40])
``````

we can indeed see that for the second function call, we get 6. The counting just keeps going instead of resetting.

Hi !
I hope I could find some explanation here because I’m stuck.

I don’t understand why this message appears when I run my code

" `divisible_by_ten([])` should have returned `0` , and it returned None "

while what is returned is actually “3”.

My code is not the best one but it works (I also put !=0 to see how many not divisible numbers were found with solution and the result is also correct (2)

``````def divisible_by_ten(nums):
div_by_ten = 0
for r in range(len(nums)):
if r <= len(nums) - 1 :
for i in nums:
if i % 10 == 0:
div_by_ten += 1
else:
continue
return div_by_ten

#Uncomment the line below when your function is done
print(divisible_by_ten([20, 25, 30, 35, 40]))
``````