0
0
Pythonprogramming~10 mins

Why scope matters in Python - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the value of the variable inside the function.

Python
def greet():
    message = 'Hello!'
    print([1])
greet()
Drag options to blanks, or click blank then click option'
Aprint
Bgreet
Cmessage
DHello
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the function name instead of the variable.
Trying to print a string without quotes.
Using a variable not defined in the function.
2fill in blank
medium

Complete the code to access the global variable inside the function.

Python
count = 5
def show_count():
    print([1])
show_count()
Drag options to blanks, or click blank then click option'
Acount
B5
Cshow_count
Dprint
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the function name instead of the variable.
Trying to print the number directly without the variable.
Using a variable not defined globally.
3fill in blank
hard

Fix the error by completing the code to modify the global variable inside the function.

Python
counter = 10
def increase():
    global [1]
    counter += 1
increase()
print(counter)
Drag options to blanks, or click blank then click option'
Acounter
Bincrease
Ccount
Dglobal
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a different variable name in the global statement.
Forgetting the global keyword.
Trying to modify the variable without declaring it global.
4fill in blank
hard

Fill both blanks to create a local variable and print it inside the function.

Python
def show():
    [1] = 'Local'
    print([2])
show()
Drag options to blanks, or click blank then click option'
Atext
Bmessage
Dprint
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using different variable names for assignment and print.
Trying to print a function name.
Using a keyword like print as a variable.
5fill in blank
hard

Fill all three blanks to create a global variable, modify it inside a function, and print the updated value.

Python
value = 3
def update():
    global [1]
    [2] += 7
update()
print([3])
Drag options to blanks, or click blank then click option'
Avalue
Dupdate
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using different variable names in different places.
Forgetting the global keyword.
Trying to print the function name instead of the variable.