I am concerning when using global variable with Def function.
Request: Count how many numbers in the list can be divided by ten.
CORRECT answer
def divisible_by_ten(nums):
count_number = 0
for num in nums:
if (num % 10 == 0):
count_number += 1
return count_number
print(divisible_by_ten([20, 25, 30, 35, 40])) #will return 3
1st question: What happens if I set the variable of count_number in the very beginning/before the def function?
I tried and result return 1, which is incorrect.
As I understand that case means the variable is global one and can we use anytime even call it in the end of function?
2nd question: If I set the variable after the line of the loop “for…in”, result also returns 1, which is incorrect.
Does it seem to be running same way as of the case in my 1st question?
If not, what is the difference and how the result comes like that?
Could anyone help me with that? I am trying to search google but still getting confused and more confused.
Much appreciate any helps.
For question 1 do you mean like this?
count_number = 0
def divisible_by_ten(nums):
for num in nums:
if (num % 10 == 0):
count_number += 1
return count_number
print(divisible_by_ten([20, 25, 30, 35, 40]))
If so that wouldn’t work because although you can access a global variable in the function to set it you would need to use the global keyword first, like so:
count_number = 0
def divisible_by_ten(nums):
global count_number
// rest of code
The big difference is that doing it like this, count_number
keeps it’s value for each call and doesn’t start at 0 each time. So in your example, it would start at 0 then after calling it, it would be 3 and if you called it again with the same input it would be 6.
But I don’t think this is what you mean as you said it returns 1, so you would have to show an example.
With regard to question 2, do you mean like this?
def divisible_by_ten(nums):
for num in nums:
count_number = 0
if (num % 10 == 0):
count_number += 1
return count_number
print(divisible_by_ten([20, 25, 30, 35, 40]))
That will return 1 for you because the last item (40) is divisible by 10, if you changed it to something not divisible by 10 it would always be 0. It is only really reporting the last item, because when you are looping over each number you reset count_number to be 0 and then add one if it is divisible by 10. When it goes before the for each, it is set once and then for each item you just add one or do nothing to it.
1 Like
Thank you so much for your explaination. I got the points you are telling. It really helps me! 
- For question 1: you are right. it even does not work. Sorry for the confustion of my question.
However, I am stille confused a little about global variable and found other example for it.

After this example, I can understand the way of using global variable. Like, global variable can be used but we NEED to call it again after def function to start the value of the variable before getting in to the loop. It is correct?
Another question that about the bracket of num % 10 == 0. I did not use the bracket before and the answer return 1. Could you please help me explain how it is running to get that answer of 1?
def divisible_by_ten(nums):
count_number = 0
for num in nums:
if num % 10 == 0:
count_number += 1
return count_number
print(divisible_by_ten([20, 25, 30, 35, 40]))
Sorry that I tried to attach more screenshot but only 1 attachment is allowed for new users 
I accidently added the () in if (num % 10 == 0):
because I was mixing the syntax of python and other languages. It isn’t required to make it work, but does work because of the order of operations, just like in maths - but it doesn’t change the result at all.
def divisible_by_ten(nums):
count_number = 0
for num in nums:
if num % 10 == 0:
count_number += 1
return count_number
print(divisible_by_ten([20, 25, 30, 35, 40]))
That code is correct, I even double checked and it does return 3. So I can’t help with you seeing 1, I’m guessing something was/is different in the code returning 1.
With regards to the global variable, the picture you posted is wrong.
def greet():
# declare local variable
print('Local", message)
That is not declaring a local variable, that is just using the global one.
# dec;lare global variable
global_variable = "Global"
def use_global_variable():
# Use access global_variable's value
print(global_variable)
def edit_global_variable():
# use global key word to enable us to edit the value of global_variable
global global_variable
global_variable = "New Value"
edit_global_variable()
print(global_variable) # New Value
def declare_global_variable():
# global keyword can also be used to create a variable not at the top level but make it a global variable
global new_global
new_global = "Another Global"
# call the function that creates the new global variable
declare_global_variable()
# use the value of new_global
print(new_global)
def declare_local_variable():
# declare a local variable with the same name as the global variable without the global keyword will hide the global variable
global_variable = "Actually local"
primt(global_variable) # Actually local
declare_local_variable()
print(global_variable() # Global or "New Value" if edit_global_variable called already and not changed to "Actually Local"
The above should demonstrate how the global variables and the global keyword works. When using a variable in a function (or more local scope) it will check if the variable exists locally, and if it does it will use it otherwise it will check globally. When setting a variable, if it hasn’t been set locally it will not use the global variable, even if it exists unless it is declared locally with the global keyword. The reason you can get the value of a global variable but not set it without the keyword is to avoid accidently changing its value.
1 Like