Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a function that takes one argument and prints it.
Python
def greet([1]): print(f"Hello, {name}!") greet("Alice")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using a different parameter name than the one used inside the function causes a NameError.
โ Incorrect
The function parameter should be named 'name' because the print statement uses 'name'.
2fill in blank
mediumComplete the code to define a function that adds two numbers and returns the result.
Python
def add_numbers(a, [1]): return a + b result = add_numbers(3, 4) print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using a parameter name that does not match the variable in the return statement causes a NameError.
โ Incorrect
The second parameter must be named 'b' because the return statement uses 'b'.
3fill in blank
hardFix the error in the function call by completing the argument type correctly.
Python
def repeat_text(text, times): return text * times result = repeat_text("Hi", [1]) print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Passing the number as a string causes the function to repeat the string incorrectly or raise an error.
โ Incorrect
The second argument must be an integer (3) to multiply the string correctly.
4fill in blank
hardFill both blanks to create a function that filters numbers greater than 5 from a list.
Python
def filter_numbers(numbers): return [num for num in numbers if num [1] [2]] result = filter_numbers([2, 7, 4, 9]) print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using '<' instead of '>' will filter numbers less than 5.
Using the wrong number in the comparison changes the filter result.
โ Incorrect
The condition 'num > 5' filters numbers greater than 5.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps words to their lengths if length is greater than 3.
Python
words = ["apple", "cat", "banana", "dog"] lengths = { [1]: [2] for word in words if len(word) [3] 3 } print(lengths)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using '<' instead of '>' changes the filter condition.
Swapping key and value causes incorrect dictionary structure.
โ Incorrect
The dictionary comprehension maps each word to its length if the length is greater than 3.