0
0
Pythonprogramming~10 mins

Global scope in Python - Interactive Code Practice

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

Complete the code to print the global variable count inside the function.

Python
count = 10

def show_count():
    print([1])

show_count()
Drag options to blanks, or click blank then click option'
Aglobal
Blocal_count
C10
Dcount
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Trying to use a variable name that is not defined.
Using the keyword 'global' instead of the variable name.
2fill in blank
medium

Complete the code to modify the global variable score inside the function.

Python
score = 5

def increase_score():
    global [1]
    score += 1

increase_score()
print(score)
Drag options to blanks, or click blank then click option'
Avalue
Bscore
Cpoints
Dcount
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Forgetting to declare the variable as global before modifying it.
Using a different variable name in the global statement.
3fill in blank
hard

Fix the error by completing the code to correctly print the global variable name inside the function.

Python
name = "Alice"

def greet():
    print([1])

greet()
Drag options to blanks, or click blank then click option'
Aname
BName
C"name"
Dglobal name
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a capitalized variable name Name which is undefined.
Trying to print the string "name" instead of the variable.
4fill in blank
hard

Fill both blanks to create a dictionary with word lengths only for words longer than 3 letters.

Python
words = ["apple", "cat", "banana", "dog"]
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the word itself instead of its length.
Using less than symbol which filters shorter words.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values only for positive values.

Python
data = {"a": 1, "b": -2, "c": 3}
result = { [1]: [2] for k, v in data.items() if v [3] 0 }
print(result)
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the original key without uppercase.
Filtering values less than or equal to zero.