0
0
Pythonprogramming~10 mins

Local 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 define a local variable x inside the function.

Python
def my_function():
    [1] = 10
    print(x)
Drag options to blanks, or click blank then click option'
Ax
Bz
Cy
Dvalue
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a different variable name than the one printed.
Defining the variable outside the function.
2fill in blank
medium

Complete the code to return the local variable result from the function.

Python
def calculate():
    result = 5 + 3
    return [1]
Drag options to blanks, or click blank then click option'
Avalue
Bresult
Ctotal
Dx
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Returning a variable not defined inside the function.
Returning a variable with a different name.
3fill in blank
hard

Fix the error by completing the code to access the local variable count inside the function.

Python
def counter():
    count = 0
    count += 1
    print([1])
Drag options to blanks, or click blank then click option'
Avalue
Bcounter
Ctotal
Dcount
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Trying to print a variable not defined inside the function.
Using the function name instead of the variable.
4fill in blank
hard

Fill both blanks to create a dictionary with keys as words and values as their 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}
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 as the value instead of its length.
Using the wrong comparison operator.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values only if the value is greater than 0.

Python
data = {'a': 1, 'b': 0, 'c': 3}
result = { [1]: [2] for k, v in data.items() if v [3] 0 }
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Not converting keys to uppercase.
Including values that are zero or less.
Swapping keys and values.