

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Scope
incomplete
2: Global Scope
incomplete
3: Scope Quiz
incomplete
This lesson's interactive features are locked, please to keep using them
Scope refers to where a variable or function name is available to be used. For example, when we create variables in a function (such as by giving names to our parameters), that data is not available outside of that function.
def subtract(x, y):
return x - y
result = subtract(5, 3)
print(x)
# ERROR! "name 'x' is not defined"
When the subtract function is called, we assign 5 to the variable x, but x only exists in the code within the subtract function. If we try to print x outside of that function, then we won't get a result. In fact, we'll get a big fat error.
Click to play video
Find the bug in the code on line 10. We're using variable names from the wrong scope. Fix it!