Python Code Challenges. Loops. Help with function scope question

Hello Community,
I do not understand why my var counter is not being recognized within the function.
I know I can move it into the function and it will work fine, but based on what I’ve learned, a local scope variable should work within the function without an issue.

Why is this not working??

counter = 0 print(counter) def divisible_by_ten(nums): for num in nums: if num % 10 == 0: counter += 1 else: False return counter print(divisible_by_ten([20, 25, 30, 35, 40]))

https://www.codecademy.com/courses/learn-python-3/articles/python-code-challenges-loops

Hi there and welcome to the forums!

The variable counter currently is a global scope variable, not local scope. It’s available throughout the entire file rather than just within the function. However in most other programming languages, you have a keyword to declare variables which allow the program to differentiate between global variables and new local variables with the same name. However Python doesn’t have such a keyword, and as a result can’t tell the difference between global and local variables with the same name. Therefore the way it works is essentially by defaulting to local scope always for checking unless specifically specified.

What this means practically is that on line 6 where you try to add 1 to counter, it’s looking for a local variable inside the function named counter, finding nothing and throwing an error. The best way of dealing with this would simply be moving counter inside, however if you want the counter to be global you need to use the global keyword inside the function to let the function know it needs to check the global scope for counter.

counter = 0
def divisible_by_ten(nums):
    global counter
    for num in nums: // etc
    // The rest of your function code is here

This will make python find the right variable and fix the code issues.

Thank you for the response.

However, I’m still not understanding. Especially when this exact lesson makes it sound like this should work.

Please, observe the following details from lesson:
We call the part of a program where destination can be accessed its scope. The scope of destination is only inside the trip_welcome().

Take a look at another example. Everything BELOW is all from the Python lesson.
https://www.codecademy.com/courses/learn-python-3/lessons/intro-to-functions/exercises/variable-access

budget = 1000 # Here we are using a default value for our parameter of `destination` def trip_welcome(destination="California"): print(" Looks like you're going to " + destination) print(" Your budget for this trip is " + str(budget)) print(budget) trip_welcome()

Our output would be:

1000Looks like you're going to California Your budget for this trip is 1000

Here we are able to access the budget both inside the trip_welcome function as well as our print() statement. If a variable lives outside of any function it can be accessed anywhere in the file.

Thank you for the response. However, I still do not understand, given that this lesson specifically states that it should be possible. What am I not understanding?

https://www.codecademy.com/courses/learn-python-3/lessons/intro-to-functions/exercises/variable-access

budget = 1000 # Here we are using a default value for our parameter of `destination` def trip_welcome(destination="California"): print(" Looks like you're going to " + destination) print(" Your budget for this trip is " + str(budget)) print(budget) trip_welcome()
1000Looks like you're going to California Your budget for this trip is 1000

Here we are able to access the budget both inside the trip_welcome function as well as our print() statement. If a variable lives outside of any function it can be accessed anywhere in the file.

Apologies, so to clarify the difference, in your first function you are trying to modify the variable using an operator. This is where the issue lies when using global variable, as you can access it and display the value, like you do with the budget code. However you cannot modify it without using the global keyword like I did. Otherwise it tries to find a global variable to modify, cannot and throws the error. You can notice this if we modify your budget code:

budget = 1000 # Here we are using a default value for our parameter of `destination` def trip_welcome(destination="California"): print(" Looks like you're going to " + destination) print(" Your budget for this trip is " + str(budget)) budget -= 50 print(budget) print(budget) trip_welcome()

This throws an error for line 6, because as soon as you try to modify the variable aka. assign to the variable at all within the function, it makes it a local variable. Therefore the function is seeing the local variable assigned on line 7, but the local variable is referenced on line 6 now which is producing the error. But if we never attempt to assign the variable in the function, it works perfectly, as it looks to the global variable.

Now if we look at your first code, we can change this to:

counter = 0 print(counter) def divisible_by_ten(nums): for num in nums: if num % 10 == 0: counter = 1 else: False return counter print(divisible_by_ten([20, 25, 30, 35, 40])) print(counter)

Notice how this works, but the print on line 12 gives 0, whilst the one returned from the function gives 1. That’s because these are now two different variables, and we are simply creating a new local variable called counter. Now replace counter = 1 with counter += 1 and see how it throws the error now? It’s trying to reference a local variable counter as part of the modification, however no such variable has been initialised. Therefore it now no longer works.


TL;DR

The big summary of this is essentially that you cannot modify global variables inside functions without using the global keyword to initialise them in the function. If you just want to use them to modify a new, local variable, or to print their value that’s all acceptable. But a global variable cannot be modified unless the global keyword is used to initialise it first.

1 Like

Thank you for taking the time to describe in detail. I read through it many times, and I am content with this answer. I can see a little more about how this works in Python.

So, without using the global keyword before my variable, it considers my variable to be a local var for the function.

However, I noticed also now that the coder can print a variable that was created outside the function scope, but apparently it cannot be a number type object. You must use the str() function in order for it to print properly.

That pushes my knowledge further into my questions, but for now my original question has been answered. Thank you!

V/r,
greyhatdude

1 Like