Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
This Reading and changing attributes with dot notation worksheet is part of the ACARA Digital Technologies Years 9–10 — Modular and Object-Oriented Programming (Python) journey. Print it as a booklet?
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1print(pet.name) | AThe attribute name is misspelled, so this creates a new wrong attribute and the real one stays unchanged, or reading it later gives an AttributeError. | |
| 2pet.age = 5 | Bprint(pet.colour) | |
| 3Each object keeps its own attribute values, so changing one object does not change another | CReads the name attribute stored in the pet object and displays it | |
| 4Reading an attribute that the object does not have causes an AttributeError | Ddog.age = 7 print(cat.age) | |
| 5print(pet[name]) | ESquare brackets are used instead of dot notation. An attribute is read with pet.name, not pet[name]. | |
| 6pet.nmae = 5 | FChanges the age attribute of the pet object to 5 |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
object.attribute — Dot notation reads or changes an attribute stored in an object, like print(pet.name) or pet.age = 5. Each object keeps its own values. A missing attribute causes an AttributeError.pet = Pet("Milo", 4)
print(pet.name)pet = Pet("Milo", 4)
pet.age = 5dog = Pet("Rex", 3)
cat = Pet("Kit", 2)
print(cat.name) # prints Kitpet = Pet("Milo", 4)
print(pet.colour) # AttributeErrorpet = Pet("Milo", 4)
pet.age = pet.age + 1
print(pet.age) # 5