FAQ: Code Challenge: Loops - Divisible by Ten

Without being able to tell, for sure, this sounds like a return inside the loop. Unindent the return line so it matches with count = 0.

I think i’ve got an elegant solution but I’m wondering if i should make a new variable for the lenght of the list or is it ok to just return len(list)?

def divisible_by_ten(nums):
  divisible = [num for num in nums if num % 10 == 0]
  return len(divisible)
2 Likes

If list comprehensions have not been taught up to this point, then one should probably venture into other possible solutions that make use of what has been taught so far. Don’t skip the naive training, or you miss out on some important discoveries and hidden details.

As for your question, an intermediary variable is not really necessary since it is only the return. len(list) is ample.

1 Like

Thanks! We had comprehension a few lessons before so that’s why I wanted to use it.

1 Like

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

When I use the following code the output is 1 - the correct answer is 3:

#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]))

The below is the code from the solution. The only difference is the indentation of the return count line. Why would the more indented line run the code over less numbers?
TIA

#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]))

Peter, any return statement does two things:

  1. Places the return value in memory - where it can be grabbed by the calling statement and assigned to a variable or used in another function.

  2. Halts the execution of the function. Once return is called one time, the function is terminated.

Ok. I get the first part.

But in this instance when return is indented to a different degree it halts the function at different points.

So when it is indented such that it sits within the IF clause it halts the function after one IF iteration and when its indented such that it its outside the IF clause and underneath the FOR clause, the FOR clause does run through all the elements and does not halt?

A colon represents the opening of a new block, hence the indent to isolate it from the current block (the one containing the signature line). When a return is encountered anywhere in the code, the function ceases and program flow resumes at the caller.

Sometimes, as above, we want and expect the loop to fully iterate the object without exiting the function. The return would be outside of both the if and the for..in blocks, and would be written in the same block as the for signature line.

for ...:
    if ...:
        # ...
return ...

You need to carefully follow the logic of the code. When return is reached ONE TIME, the function terminates. The key is to keep track of when you are in a loop, and when out. Learn how to read “blocks”, as @mtf described above.

if does not iterate (move sequentially from one value to the next). An if statement may or may not execute, depending upon whether its condition returns True. If it executes, and a return statement is lurking beneath it, the function will halt.

for loops, on the other hand, do iterate. Sometimes there are if statements beneath them. If, during one of the iterations, one of those if conditions returns True, and leads to return, again, the function halts.

Thanks for the explanation on the count = 0. For purposes of memory retention what specifically should I use to help me remember this, for example is it because the word ‘for’ is used and typically or I guess always?? precedes a loop and the function contains a variable name ‘nums’? I’m just having a hard time coding from scratch without looking answers up. The rest of the code makes sense to me. Why doesn’t python program have a build in a default understanding that index positions start at 0…? Ugh…

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

That is absolutely the default!

The need for count in this function has nothing to do with indexing, nor specifically with the fact that a for loop is being used. count is simply a variable whose job it is to keep track of how many times something happens, in this case, how many numbers are divisible by ten.

In some versions of C, I believe, (and maybe in other languages), simply declaring an int variable sets its value at zero, but that is a different issue than indexing, and in Python, we don’t declare variables, we just jump right in with them. In this case we wanted count to be initialized at zero, but it could have been any number, perhaps a value passed by another function.

For future reference, if you are going to perform an operation on a variable, the variable must have been previously defined (assigned a value.) Otherwise, you’ll get a NameError.

count is not an index, just a running tally. The for loop above is not looking at indexes, but values. I like to call it a read-only loop since we are not manipulating the data, only polling it and checking its parity. count is how many even numbers there are in the list, nums.

Whenever we use for it is on an iterable such as a list, a range, or one of the other data structures, including strings. We can access the iterable by index, or by value. Above, number is one value at a time from nums.