0
0
Pythonprogramming~10 mins

Methods with parameters 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 method that takes one parameter and prints it.

Python
class Printer:
    def print_message(self, [1]):
        print(message)

p = Printer()
p.print_message("Hello")
Drag options to blanks, or click blank then click option'
Amessage
Bmsg
Ctext
Dinput
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name different from the variable inside the method causes a NameError.
2fill in blank
medium

Complete the code to call the method with the correct argument.

Python
class Calculator:
    def add(self, number):
        return number + 10

calc = Calculator()
result = calc.add([1])
print(result)
Drag options to blanks, or click blank then click option'
Anumber
B"5"
C5
Dadd
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string instead of a number causes concatenation or errors.
3fill in blank
hard

Fix the error in the method definition to correctly accept two parameters.

Python
class Multiplier:
    def multiply(self, [1]):
        return a * b

m = Multiplier()
print(m.multiply(3, 4))
Drag options to blanks, or click blank then click option'
Ax, y
Ba, b
Cself, a, b
Dnum1, num2
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting self causes a TypeError when calling the method.
4fill in blank
hard

Fill both blanks to create a method that returns the sum of two parameters.

Python
class Adder:
    def add(self, [1], [2]):
        return x + y

adder = Adder()
print(adder.add(7, 8))
Drag options to blanks, or click blank then click option'
Ax
By
Ca
Db
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameter names different from the variables in the return statement causes NameError.
5fill in blank
hard

Fill all three blanks to create a method that returns a formatted string using its parameters.

Python
class Greeter:
    def greet(self, [1], [2]):
        return f"Hello, [3] [2]!"

g = Greeter()
print(g.greet("Dr.", "Smith"))
Drag options to blanks, or click blank then click option'
Atitle
Bname
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up parameter names or placeholders causes NameError or wrong output.