Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to call the method greet on the object person.
Python
class Person: def greet(self): print("Hello!") person = Person() person.[1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong method name that does not exist.
Forgetting the parentheses to call the method.
✗ Incorrect
The method defined is named
greet, so calling person.greet() runs it.2fill in blank
mediumComplete the code to call the method add with argument 5 on the object calc.
Python
class Calculator: def add(self, x): return x + 10 calc = Calculator() result = calc.[1](5) print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist.
Forgetting to pass the argument inside parentheses.
✗ Incorrect
The method to add is named
add, so calling calc.add(5) returns 15.3fill in blank
hardFix the error by completing the code to call the method display on the object obj.
Python
class Display: def display(self): print("Showing content") obj = Display() obj[1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Missing the dot before the method name.
Missing the parentheses to call the method.
✗ Incorrect
To call a method, you need the dot and parentheses:
obj.display().4fill in blank
hardFill both blanks to complete the method call that returns the length of the string stored in obj.text.
Python
class TextHolder: def __init__(self, text): self.text = text obj = TextHolder("hello") length = len(obj.text[1]()) if obj.text [2] "" else 0 print(length)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators.
Using a method that does not exist on strings.
✗ Incorrect
We call
strip() to remove spaces, and check if obj.text != "" to ensure it's not empty.5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps each word to its uppercase form if the word length is greater than 3.
Python
words = ["apple", "cat", "banana", "dog"] result = { [1]: [2] for [3] in words if len(word) > 3 } print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces around the key incorrectly.
Mixing up the key and value positions.
Using wrong variable names.
✗ Incorrect
The dictionary comprehension uses
{word: word.upper() for word in words if len(word) > 3} to map words longer than 3 letters to uppercase.