0
0
Pythonprogramming~10 mins

Procedural vs object-oriented approach in Python - Interactive Practice

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

Complete 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'
A+
B-
C*
D/
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.
2fill in blank
medium

Complete 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'
ANumber
BFunction
CAdd
DCalculator
Attempts:
3 left
💡 Hint
Common Mistakes
Using a lowercase or unrelated name for the class.
Using 'Function' which is not a class name here.
3fill in blank
hard

Fix 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'
A__init__
Bself
Cadd
Dcalculate
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.
4fill in blank
hard

Fill 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'
ACalculator
Badd
Ccalculate
DFunction
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong class name or method name.
Calling a method that does not exist.
5fill in blank
hard

Fill 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'
Aadd
Cadd_numbers
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for both procedural function and method.
Calling a function that is not defined.