FAQ: Code Challenge: Loops - Divisible by Ten


#1

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!


#2

When creating a function and extract a particular integer or string from the list, why is it necessary to create a new variable? Below is the code:

#Write your function here
def divisible_by_ten(nums):
count = 0
for number in nums:
if (number % 10 == 0):
count += 1
return count

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


#3

count = 0

that’s where i get stuck


#4

The loop is iterating values in the list. count is tracking their positions in the list, by index. Recall that lists are zero-indexed using positive integers (and zero) in sequence. The highest index is always one less than the length of the list.


#5

Oh ok. I got it! Yes thank you


#6

I’m not a native speaker and I’m oftenly answering wrong because the statement is not clear.

Return the amount of numbers in that list that are divisible by 10

That wasn’t clear for me and I was trying to sum everything


#7

How’s this?

Return a count of the numbers in that list which are divisible by 10.

#10

Is there any difference if I run my function on a (list) instead of (nums)? Under what circumstances could it matter? Thank you.
def divisible_by_ten(nums): or def divisible_by_ten(list):


#11

nums is the local variable referencing the list literal that is passed into the function. The same would apply if a list object is passed in by name. What name we give it locally is arbitrary, but it should describe the list, rather than just call it list. At least nums tells us something about the expected contents of the input.


#12

Hi,

I tried that but i don’t know if i have to use a while loop but anyway the count doesn’t stop to 3…

def divisible_by_ten(nums):
count = 0
while count <= len(nums):
for num in nums:
if num % 10 == 0:
count += 1
return count


#13

This will result in an infinite loop since the length of the list never changes. You have a for loop that iterates the list and checks each value for divisibility by 10. That is all you need.


#14

Thanks Roy, I changed it and it worked !


#15

Hello,
Given what the lessons are, how are we supposed to figure out that we’re supposed to create a new variable ‘count’, to use ‘number’ in nums etc…
I feel like switching from being over protected to being thrown into the wild without a notice…


#16

Not sure I follow your question, though I sense some vexation. When it becomes necessary to calm down, nothing does more good than a kilometer walk.

I always have music going through my head so don’t need a walkman, just the beat of my steps. Steps are taken one at a time, and are generally quite in sync with one another, hence a beat.

The general sameness of each step (or pair of steps to arrive at the same foot) can be thought of as a unit measure of time, and of distance. Once we know the length of each step we may count off the number of steps and compute the overall distance. Ditto for time. Elapsed time divided by number of steps gives time per step. I’m around allegretto/allegro time (~120 steps a minute over which I cover 100 m).

The act of counting is really just a finite number of additions of 1 to arrive at a total count. Bring on the variable, count, which by the above, says what it represents. The number of items counted.

Counting generally starts from a position of neutrality; that is from a point that has no value that may affect the outcome. In arithmetic we call this the additive identity and its value is 0.

If we construct a sequence of additions,

0 + 1 + 1 + 1 + 1 + 1 + 1 + 1

with the first term set to zero, the final outcome will be the same as when that term is not included.

What we see above is counting, so count would be the logical variable to reference its present value.

A variable name nums is very suggestive of a list of numbers by the plurality inferred. One with the name number will infer singularity. This is what is meant by semantic naming convention; we give concise and explicit meaning to our variable names.

count implies a number, as we’ve discussed, but number does not imply a count. It’s just number plucked out of somewhere. We get that meaning from its name.

Now given a list of numbers at random, this exercise expects us to count the number of them that are divisible by 10. We start with a count of zero, as discussed, and then iterate over the list, dividing each by 10 and seeing if there is a remainder or not. If not, we add 1 to our count.

From here one expects you’ll do smashingly on the exercise, and have some food for thought to carry forward. Happy coding!


P. S.

In case you were wondering, the number that we ADD to a number that gives us the additive identity is called the additive inverse. Can you guess what it is?

Hint: Above we have a count of 7. What number will take us back to zero if we add it?


#17

Hello CC Moderator:

I have not used Count variable as most of the people here did (although I realize that it is simpler).

I have used the following to return the amount of numbers that are divisible by 10:

list_w_num_by_10 = []
def divisible_by_ten(nums):
  for number in nums:
    if number % 10 == 0:
      list_w_num_by_10.append(number)
      return
    len(list_w_num_by_10)

print(divisible_by_ten([20, 25, 30, 35, 40]))
print(list_w_num_by_10)
# prints: 
None
[20]

I do not understand why .append(number) is not adding number that satisfies the if statement into the list_w_num_by_10 list? It returns None even though there should be at least one number, 20, given the second print call.

Thank you for your insight!


#18

This should be initialized inside the function so it is an empty list each time the function is run.

Be sure to unindent out of your loop before returning.

return len(...)

Note that return by itself is the same as not having a return. In either case it will return None.


#19

Hello @mtf !

Thank you for your prompt attention and reply!

This should be initialized inside the function so it is an empty list each time the function is run.

Be sure to unindent out of your loop before returning.

I did that (sorry - it was my typo that len() was unindented initially).

Result now returns 1 and I don’t know why?
Could you, please also briefly explain why list_w_num_by_10 = [] had to be defined within the function? Does it matter in this case whether it is defined globally or locally within divisible_by_ten function if .append() will change that list anyway?

Thank you for your time and further help!


#20

Because each time we call the function, we want to start with an empty list to accumulate to (or a counter, alternatively).

A counter would be more practical since it is a single value, rather than a data structure. It is likely quicker to add 1 than to append to a list then poll the length. However, for this exercise, either should be fine.


#21

" Return the amount of numbers in that list that are divisible by 10"

I think this statement is grammatically wrong. “Amount” is used for uncountable nouns. Hope they will fix that soon. It’s confusing.


#23

Hi

Could someone help me? I dont understand why my code below only returns 1 when it should be returning 3?

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

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