Complete the code to define a method that takes one parameter and prints it.
class Printer: def print_message(self, [1]): print(message) p = Printer() p.print_message("Hello")
The method parameter must match the variable used inside the method. Here, message is used, so the parameter should be message.
Complete the code to call the method with the correct argument.
class Calculator: def add(self, number): return number + 10 calc = Calculator() result = calc.add([1]) print(result)
The method expects a number, so passing the integer 5 is correct. Passing a string "5" would cause unexpected behavior.
Fix the error in the method definition to correctly accept two parameters.
class Multiplier: def multiply(self, [1]): return a * b m = Multiplier() print(m.multiply(3, 4))
self causes a TypeError when calling the method.Methods must have self as the first parameter, which is already included. To accept two numbers, fill the blank with a, b to match the variables used inside the method.
Fill both blanks to create a method that returns the sum of two parameters.
class Adder: def add(self, [1], [2]): return x + y adder = Adder() print(adder.add(7, 8))
The method parameters must match the variables used in the return statement. Here, x and y are used, so the parameters should be x and y.
Fill all three blanks to create a method that returns a formatted string using its parameters.
class Greeter: def greet(self, [1], [2]): return f"Hello, [3] [2]!" g = Greeter() print(g.greet("Dr.", "Smith"))
The method parameters are title and name. The formatted string uses title and name. The third blank is the first part inside the string, which is title.