FAQ: Code Challenge: Loops - Divisible by Ten

This community-built FAQ covers the “Divisible by Ten” exercise from the lesson “Code Challenge: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Divisible by Ten

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

16 posts were split to a new topic: Why do I need to create a new variable (like count?)

3 posts were split to a new topic: Code challenge loops wording

3 posts were split to a new topic: Stuck in an infinite loop

14 posts were merged into an existing topic: Indentation level of return

3 posts were split to a new topic: Solving the code challenge with list comprehension

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?

Based on other constructive feedback I’ve gotten from previous chapters, does the following solution scale or is this bad code hygiene?

def divisible_by_ten(nums):
  return len([i for i in nums if i%10==0])

Comprehensions are scalable, with the only constraint being available memory. Your solution is the same as what I wrote way back when, which should tell you that I never once considered ‘hygiene’. I like expressive solutions as opposed to imperative, but that doesn’t mean I have enough expertise to warrant an opinion.

As code simplicity is concerned, the comprehension is the simplest. Consider,

return len(list(filter(lambda n: not n % 10, x)))

More code, possibly more complexity, but one minor advantage… It may use less memory. Something to look into.

Why did this code only print 20?

I was experimenting with the code and was trying to see how I could make it only print out the numbers that were divisible by 10. As you see it only went to 20 and stop, why did it do that?

return in an if, or just in a loop will terminate the function. If you wish to print all the numbers, then print instead of return.

Why doesn’t this work?
#Write your function here
def divisible_by_ten(nums):
for num in nums:
if num % 10 == 0:
num = 10
return nums.count(10)

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

Hey @6sun, welcome to the forums!

Why are you doing it like this? Why don’t you just use a variable to increment the number of tens?

Here’s why this doesn’t work though:

x = [1,2,3,4,3]
y = x[0]
y = 3

-> [1,2,3,4,3]
-> 3

This is basically what you’re doing. I don’t think it could work.

I know how to use a variable to do it but I still want to know why this doesn’t work.
I was trying to turn [20, 25, 30, 35, 40] into [10, 25, 10, 35, 10] and use .count(10) to count.
What is wrong with it? I still don’t get it.

i think there are two parts to this answer, first, using the for loop like you do:

for num in nums:

now num will hold read-only values of the list, any changes made to num won’t persist

to update elements in a list, use an index:

the_list[index] = 'new value'

as for the second part, i wouldn’t use .count(), you already have a loop, so why not use that to count the number of occurrences?

1 Like