Setter methods that validate data

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Setter methods that validate data

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What is the main job of a validating setter such as set_age(self, age)?
A Check the new value before storing it in the private attribute
B Store any value first and check it later
2. Which setter body keeps an age valid?
A if 0 <= age <= 120: self._age = age
B self._age = age; if age < 0: print('Bad')
3. A student calls set_age(-5) on a Person object. What should a well-written setter do?
A Reject the value and leave _age unchanged, reporting the problem
B Store -5 in _age anyway
4. Which range check correctly allows ages from 0 to 120 only?
A if age >= 0 or age <= 120:
B if age >= 0 and age <= 120:
5. Why use a setter with validation instead of writing person._age = 500 directly?
A Direct assignment always checks the range for you
B The setter can stop invalid values like 500 from being stored

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight 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

Definitions

def set_age(self, age):

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
def _______(self, age): if 0 <= age <= 120: self._age = age
2.
if age >= 0 ___ age <= 120: self._age = age else: print("Bad age")
3.
if 0 <= age <= 120: self.____ = age else: print("Bad age")
4.
if age < 0 or age > 120: _____ ValueError("Bad age") self._age = age
5.
if age < 0 or age > 120: ______ False self._age = age

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A def set_age(age):
B if 0 <= age <= 120:
C self._age = age
D else:
E print('Invalid age')
Question 2
A def set_score(self, score)
B if 0 <= score <= 100:
C self._score = score
D else:
E print('Bad score')
Question 3
A def set_age(self, age):
B if age < 0 or age > 120:
C print('Invalid age')
D pass
E self._age = age
Question 4
A def set_age(self, age):
B if 5 <= age or age <= 18:
C self._age = age
D else:
E print('Age 5-18 only')
Question 5
A def set_age(self, age):
B if 0 <= age <= 120:
C age = self._age
D else:
E print('Invalid age')

Answer Key

Setter methods that validate data
Section A: Quick Questions
Q1
A — Check the new value before storing it in the private attribute
Q2
A — if 0 <= age <= 120: self._age = age
Q3
A — Reject the value and leave _age unchanged, reporting the problem
Q4
B — if age >= 0 and age <= 120:
Q5
B — The setter can stop invalid values like 500 from being stored
Section B: Match the Pairs
Q1
def set_age(self, age): → Defines a setter method that receives a new age so it can be checked before being stored
Q2
if 0 <= age <= 120: → A range check inside the setter that only allows ages from 0 to 120
Q3
Only store the new value in the private attribute after the check passes → if age >= 0: self._age = age
Q4
Report a bad value instead of storing it, so the object keeps its old valid data → else: print("Invalid age")
Q5
def set_age(self, age): self._age = age if age >= 0: print("Age set") → The value is stored before it is checked, so a negative age is saved anyway. The assignment must go inside the if block.
Q6
def set_age(self, age): if 0 <= age <= 120: age = self._age → The assignment is backwards. It overwrites the parameter with the old attribute instead of storing the new age in self._age.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
def set_age(self, age): if 0 <= age <= 120: self._age = age
Q2
if age >= 0 and age <= 120: self._age = age else: print("Bad age")
Q3
if 0 <= age <= 120: self._age = age else: print("Bad age")
Q4
if age < 0 or age > 120: raise ValueError("Bad age") self._age = age
Q5
if age < 0 or age > 120: return False self._age = age
Section E: Spot the Error
Q1
A — A method must have self as its first parameter so it can reach the object's private attribute. It should be def set_age(self, age):
Q2
A — The function header is missing the colon at the end, which causes a syntax error. It should be def set_score(self, score):
Q3
D — Without return, the code carries on after reporting the error and stores the invalid age anyway. The line should be return so the bad value is rejected.
Q4
B — Using or makes the check true for almost any number, so invalid ages would be accepted. Both range limits must hold, so it should use and.
Q5
C — The assignment is the wrong way round. It overwrites the parameter instead of updating the private attribute. It should be self._age = age