Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
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 | |
|---|---|---|
| 1def set_age(self, age): | Aelse: print("Invalid age") | |
| 2if 0 <= age <= 120: | BThe value is stored before it is checked, so a negative age is saved anyway. The assignment must go inside the if block. | |
| 3Only store the new value in the private attribute after the check passes | Cif age >= 0: self._age = age | |
| 4Report a bad value instead of storing it, so the object keeps its old valid data | DThe assignment is backwards. It overwrites the parameter with the old attribute instead of storing the new age in self._age. | |
| 5def set_age(self, age): self._age = age if age >= 0: print("Age set") | EDefines a setter method that receives a new age so it can be checked before being stored | |
| 6def set_age(self, age): if 0 <= age <= 120: age = self._age | FA range check inside the setter that only allows ages from 0 to 120 |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
def set_age(self, age): — A setter method that checks a new value (such as a valid age range) before storing it in a private attribute, rejecting or reporting bad values so the object never holds invalid data.def set_age(self, age):
if 0 <= age <= 120:
self._age = ageif age >= 0 and age <= 120:
self._age = age
else: print("Bad age")if 0 <= age <= 120:
self._age = age
else: print("Bad age")if age < 0 or age > 120:
raise ValueError("Bad age")
self._age = ageif age < 0 or age > 120:
return False
self._age = age