Code Challenge in Loops (from Learn Python 3), why Count cannot be defined before the loop


I am going through the Learn Python 3 right now and got stuck in the loops code challenge. The questions is to create a function named divisible_by_ten() that takes a list of numbers named nums as a parameter, and return the count of how many numbers in the list are divisible by 10.

I wrote the following code:

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

And it gave me error. I am just curious, why the count variable has to be defined inside the loop? Please ignore the indent issue, don’t know why post didn’t keep the indentation…

Thank you!


Hi there! Welcome to the forums! :tada:

The indent issue is solved by highlighting your text and pressing the </> button.

And it gave me error

The key about the error is the error type, so you should post the error as well when you can.

The reason why you can’t access count is because of the way scope is defined in python. It is expecting count to be declared within the local scope of the function (not outside it). For this exercise, declaring it inside should be good enough.

Sometimes though you will really need to access outside variables. Some possibilities

  • If this were within a class, you might see a self.count. Within a class method you can access the overall class’s property of self.count (or self.anything).

  • you could pass a default parameter into the function definition: def divisible_by_ten(nums, count=count): or def divisible_by_ten(nums, count=0):,
    this would give you flexibility when you call divisible_by_ten().


I’ve fixed that for you. :slight_smile:


You are correct in wanting count to be initialized to 0 before the loop. However, you initialized it outside the function, as a global variable. This assignment statement, since it is inside the function where it belongs, forces any reference to count within the function to be regarded as a name that is local to the function:

        count += 1

As a result, the original initialization of count applies to a different variable with the same name that is global, and is not recognized inside the function.

You can move the initialization statement to inside the function, right after the function header, and just prior to the loop header.


Thank you all! So this is Global vs. Local variable difference. I thought Global variables could be used locally but guess if any modification defined within a function, same name or not it will be treated as a local variable for the modification, and count=0 should not be missing.

Out of curiosity, I just tried adding two lines: count=0 before the function and print(count) after the loop instead of print(function name), then it showed no error and gave me 0, the value of the global variable Count :grin:

Great to know. Thanks again all!

Aye, you seem to have the right idea. You can use global/outer nesting variables inside a function (and even alter mutable types) but assignment is always treated locally unless you use specific statements to say otherwise (global/ nonlocal but their use is fairly uncommon).

1 Like

As @tgrtim indicates, you can specifically declare a variable to be global when necessary, and then it can be used locally and still have a global scope, even when it is updated with new values within the function. However, it is important for functions to contain what is needed for them to produce correct results each time they are called. If a variable needs to be initialized each time the function is called, place the initialization within the function.

Following is an example of what to avoid, unless you would like the results to always be cumulative:

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

print(divisible_by_ten([7, 20, 4, 60, 7]))
print(divisible_by_ten([4, 30, 20, 40, 9]))



The first call to the function was fine. The second was not, because count did not get reset to 0 prior to the loop.

1 Like