odoltem.?? 11 #Here\'s a long one-you can do it! 3 #Aewrite the following class
ID: 3919763 • Letter: O
Question
odoltem.?? 11 #Here's a long one-you can do it! 3 #Aewrite the following class so that it uses getters and 4.#setters for all three variables (title, description, 5 completed). The getters should be called: getTitle, 6.#getDescription, getCompleted. The setters should be 7 #called: setTitle, setDescription, setcompleted. 8:# 9 #1n addition, the setter should check to make sure that 10 #the new value is the correct type: title and description 11 #should always be of type str, and completed should always 12 #be of type bool. If the value is not the right type, set 13#the value of the corresponding attribute to None (the 14 keyword, not the string "None"). 15 16 #To summarize (and give a to-do list): 17 -Create getters and sett@rs for each variable. 184-Check the type of the new value inside the Betters, 19 and print an error if it's the wrong type. 20 21 #Hint: You can check tc) see if a variable is a string by 22#checking the logical expression type (var)-str, where 23 #var ís the variable you're checking. For integers, use 24 #int instead of str. For floats, uBe float. For booleans, 25 use bool. 26 ? 27 Hint 2: Remember to put self before any instance variables 28 for methods you' re trying to ccess Fox example, to access 29 #the variable title from within a method, you would need to 30 wite selfititle 31 32 33 class TodoItem: 34 def init (self, title description, completed False): 35 36 37 38 self. title title self.description "description self.completedcompletedExplanation / Answer
class TodoItem: def __init__(self, title, description, completed=False): self.title = title self.description = description self.completed = completed def getTitle(self, title): return self.title def getDescription(self, description): return self.description def getCompleted(self, completed): return self.completed def setTitle(self, title): if type(title) != str: print('Error. title should be a string') title = None self.title = title def setDescription(self, description): if type(description) != str: print('Error. description should be a string') description = None self.description = description def setCompleted(self, completed): if type(completed) != bool: print('Error. completed should be a bool') completed = None self.completed = completed
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.