0
0
Pythonprogramming~10 mins

Instance methods 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 an instance method that returns the name of the person.

Python
class Person:
    def __init__(self, name):
        self.name = name
    def get_name(self):
        return self.[1]
Drag options to blanks, or click blank then click option'
Aget_name
BName
Cname
Dself.name
Attempts:
3 left
💡 Hint
Common Mistakes
Writing 'Name' with uppercase N causes attribute error.
Using 'get_name' instead of the attribute name returns the method itself, not the value.
2fill in blank
medium

Complete the code to call the instance method greet on the object p.

Python
class Person:
    def greet(self):
        return "Hello!"

p = Person()
message = p.[1]()
Drag options to blanks, or click blank then click option'
Agreet
BGreet
Cgreet()
Dgreeting
Attempts:
3 left
💡 Hint
Common Mistakes
Including parentheses inside the blank causes syntax error.
Using uppercase 'Greet' causes attribute error.
3fill in blank
hard

Fix the error in the instance method by completing the blank with the correct parameter name.

Python
class Person:
    def __init__([1], name):
        self.name = name
Drag options to blanks, or click blank then click option'
Aperson
Bthis
Ccls
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cls' which is for class methods.
Using 'this' which is not a Python keyword.
4fill in blank
hard

Fill both blanks to create an instance method that returns the person's age plus 5.

Python
class Person:
    def __init__(self, age):
        self.age = age
    def future_age(self):
        return self.[1] [2] 5
Drag options to blanks, or click blank then click option'
Aage
B+
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using minus or multiplication operators changes the meaning.
Using wrong attribute names causes errors.
5fill in blank
hard

Fill all three blanks to create a method that returns a greeting with the person's name in uppercase.

Python
class Person:
    def __init__(self, name):
        self.name = name
    def greet(self):
        return f"Hello, [1]!".[2]().[3]()
Drag options to blanks, or click blank then click option'
Aself.name
Bupper
Cstrip
Dlower
Attempts:
3 left
💡 Hint
Common Mistakes
Using lower() instead of upper().
Calling methods in wrong order causes unexpected output.