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?

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

#Write your function here
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