Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a function that adds two numbers.
Python
def add_numbers(a, b): return a [1] b
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' will subtract instead of add.
Using '*' or '/' will multiply or divide, which is not correct here.
✗ Incorrect
The plus sign + adds two numbers together in Python.
2fill in blank
mediumComplete the code to create a class named Calculator.
Python
class [1]: def __init__(self): pass
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a lowercase or unrelated name for the class.
Using 'Function' which is not a class name here.
✗ Incorrect
The class name should be Calculator to represent the object-oriented approach.
3fill in blank
hardFix the error in the method definition inside the class.
Python
class Calculator: def [1](self, a, b): return a + b
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '__init__' which is the constructor, not the add method.
Using 'self' as a method name, which is a parameter name.
✗ Incorrect
The method name should be add to describe the action of adding numbers.
4fill in blank
hardFill both blanks to create an object and call its add method.
Python
calc = [1]() result = calc.[2](5, 3)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong class name or method name.
Calling a method that does not exist.
✗ Incorrect
We create an object of class Calculator and call its add method.
5fill in blank
hardFill all three blanks to convert a procedural add function into an object-oriented method call.
Python
def [1](a, b): return a + b class Calculator: def [2](self, a, b): return a + b result1 = [3](4, 6) calc = Calculator() result2 = calc.add(4, 6)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for both procedural function and method.
Calling a function that is not defined.
✗ Incorrect
The procedural function is named add_numbers, the method inside the class is add, and the procedural function is called as add_numbers(4, 6).