What will be printed in the console when the following code is run?
DO NOT run the code, just pretend to be a computer.
def a_function(a_parameter):
a_variable = 15
return a_parameter
a_function(10)
print(a_variable)NameError
It's trying to print a variable that is local to a_function() and is only available inside the function.
What will be printed in the console when the code is run?
DO NOT run the code, just pretend to be a computer.
i = 50
def foo():
i = 100
return i
foo()
print(i)50
The print statement is outside the function foo(). So it can't access the local variable i = 100. It only sees the global i, which is equal to 50.
What will be printed in the console when the following code is run?
DO NOT run the code, just pretend to be a computer.
def bar():
my_variable = 9
if 16 > 9:
my_variable = 16
print(my_variable)
bar()16
Remember that in Python there is no block scope. Inside a if/else/for/while code block is the same as outside it.