Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

cument, you will write two Python modules. One, Persondata, contains the definition of class PersonData. An instance of the class records the age, number of

image text in transcribed
image text in transcribed
image text in transcribed
cument, you will write two Python modules. One, Persondata, contains the definition of class PersonData. An instance of the class records the age, number of children, and spouse of a person and has methods to update and access this information. The other module, Population, contains the definition of class Population. An instance of this class has a dictionary of name-data pairs, where the data is an instance of PersonData. It also has methods to update and access this information. Class Persondata has object properties age (age in years), children (number of children), and spouse (spouse's name, a string). The default for children is 0, and the default for spouse is None. If a person has no spouse, then the corresponding Persondata object has no spouse attribute. An instance of Persondata converts to a string of the form "A (age) year old person", where (age ) is the value of self.age. None of the non-special methods return anything, except for the method that outputs the attribute values, they update attribute values or add or delete the spouse attribute. Only the marries non-special method is passed an argument (the name of the new spouse) Method birthday increments the age attribute, and method new_child increments the children attribute. Method print_data takes no argument and does not return anything. It outputs the values of the age and children attributes; if the spouse attribute exists, it outputs that as well, otherwise it outputs None. Reserve two character positions for integer values that are the values of the age and children attributes, and leave plenty of space between the values. Method marries is passed the name of the new spouse and adds to self a spouse attribute with that name as its value unless self already has a spouse attribute. In that case, it raises an exception exception with a string argument of the form "Spouse exists: (spouse)" where (spouse) is the value of self. spouse. Method divorces deletes self's spouse attribute unless this attribute does not exist, in which case it raises an AttributeError exception with a string argument of the form "Not married, divorce impossible" Include in your Persondata module the following code to be executed when the module is executed as a script. name main": pi = PersonData (31, spouse="Sue", children=2) print (pl) pl.print data() pl.birthday ( ) pl.new_child() pl.divorces() pl.print_data() pl marries ("Pam") try: pi,marries "Sue") except Exception as detail: print (detail) try: pl.divorces() p1.divorces() except AttributeError as detail: print (detail) p2 = PersonData (39) p2.print_data() When your persondata module is executed as a script, it should produce the following output A 31 year old person 31 2 Sue 32 3 None Spouse exists: Pam Not married, divorce impossible 39 0 None Hints Where o is an object and a is an attribute del o. a removes attribute a from o, and hasattr(o "a") returns True if o has attribute a (and False otherwise). Turning to module Population, this imports module PersonData and defines class Population. The sole Population object attribute is people, a dictionary whose keys are persons names and values are PersonData instances for the corresponding persons. This property starts out with the empty dictionary. An instance of Population converts to a string of the form "A population with (num) people" where (num) is the number of key-value pairs in self.people. The only non-special method that returns something is len, which takes no argument and returns the number of people recorded in self.people. Method add_person is used to add a key-value pair to the self peopl dictionary. It is passed the person's name and age and optionally their number children (defaulting to 0) and spouse's name (defaulting to None). It creates an instanc of PersonData associated in the people dictionary with the name provided unless the name is already in the dictionary. In that case, it raises a NameError exception with string argument of the form "(name) already present" where (name) is the value of the name passed in. Method remove_person is passed a name and removes the key-value pair from th people dictionary with that name as key as long as there is such a pair. If not, it raises NameError exception with a "(name) not present string argument. Method print_pop outputs a table with the information on all entries in the people dictionary. The column labels are "name", "age", "children", and "spouse". invokes the print_data method on each value in the people dictionary. To test this module, include code executed only when _name main is true. In the test code, do the following. Create a (empty) population Add Ed, age 47,2 children, spouse Pam. Add Pam, age 51, 2 children, spouse Ed. Print the population. Print the number of persons. Remove Pam. Remove Ed. Print the number of persons. . Try to remove Ed again and catch the exception. Add Ed, age 45 Try to add Ed (age 45) again and catch the exception. Print the number of persons. When your Population module is executed as a script, it should produce th following output Name Age Children Spouse Ed Pam Pam 51 2 There are 2 persons recorded. There are now 0 persons recorded. remove_person: Ed not present add_person: Ed already present There are 1 persons recorded. 47 2 Ed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design For Mere Mortals

Authors: Michael J Hernandez

4th Edition

978-0136788041

More Books

Students also viewed these Databases questions

Question

1. Where will you recommend that she hold the focus group?

Answered: 1 week ago

Question

3. What might you have done differently

Answered: 1 week ago