Question: Write a function get _ address 2 that takes a list of House objects and a Person's age, as parameters, and returns the house addresses

Write a function get_address2 that takes a list of House objects and a Person's age, as parameters, and returns the house addresses where at least one person of that age lives. If there are no inhabitants in that age in any of the houses it should print the message as shown in the tests.
Example usage:
>>
houses =[]
h1= House('123 Happy St. Blackwood')
h2= House('6 Agnes Av. Aldinga')
h3= House('8 Daw St. Aldinga')
h4= House('23 Oak St. Blackwood')
h5= House('61 Gorge Av. Aldinga')
h6= House('81 Waterman St. Aldinga')
c1= Car('Ford','Mustang',2)
c2= Car('Ford','Fiesta',4)
c3= Car('Toyota','Camry',2)
c4= Car('Mazda','Cx-9',6)
c5= Car('Mazda','Cx-5',5)
c6= Car('Mitsubishi','Colt',12)
c7= Car('Toyota','Corolla',2)
c8= Car('Ford','Escape',10)
c9= Car('Ford','Raptor',8)
c10= Car('Holden','Barina',14)
p1= Person('Jim',28)
p2= Person('Ashley',31)
p3= Person('Cleo',21)
p4= Person('Jessica',18)
p5= Person('Adam',19)
p6= Person('Andrew',34)
p7= Person('Jake',20)
p8= Person('Joshua',17)
p9= Person('Amica',25)
p10= Person('Helen',34)
p11= Person('Barney',29)
p12= Person('Sandra',30)
p13= Person('John',25)
p14= Person('Patrick',26)
p15= Person('Sammy',19)
h1.add_car(c1,c2)
h2.add_car(c3)
h3.add_car(c4)
h4.add_car(c5,c6,c7)
h5.add_car(c8)
h6.add_car(c9,c10)
h1.add_inhabitant(p1,p2,p3,p4)
h2.add_inhabitant(p5,p6)
h3.add_inhabitant(p7,p8,p9)
h4.add_inhabitant(p10)
h5.add_inhabitant(p11,p12)
h6.add_inhabitant(p13,p14,p15)
houses.extend([h1,h2,h3,h4,h5,h6])
print(get_address2(houses,32))
print(get_address2(houses,25))
output:
Looks like there are no inhabitants of 32 years old.
['8 Daw St. Aldinga','81 Waterman St. Aldinga']
Here are the Classes:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return 'This is '+ self.name +','+ str(self.age)+' years old.'
def is_teenager(self):
return self.age <20
class Car:
def __init__(self, make,model,age):
self.make = make
self.model = model
self.age = age
self.owners ={}
def __str__(self):
return 'This is: '+ self.make +','+ str(self.age)+' years old.'
def add_owner(self,*o):
for i in range(len(o)):
if isinstance(o[i][0],Person) and isinstance(o[i][1],str):
self.owners[o[i][0]]= o[i][1]
else:
print('Error: cannot update the list of car owners')
break
def get_owners_list(self):
o_list =[n.name for n in self.owners.keys()]
return [[x,y] for x,y in zip(o_list,self.owners.values())]
class House:
def __init__(self, address):
self.address = address
self.inhabitants =[]
self.cars =[]
def __str__(self):
return 'This house is inhabited by '+ str(len(self.inhabitants))+' people.'
def add_inhabitant(self,*p):
for i in range(len(p)):
if isinstance(p[i],Person):
self.inhabitants.append(p[i])
else:
print('Error: cannot update the list of inhabitants')
break
def add_car(self,*c):
for i in range(len(c)):
if isinstance(c[i],Car):
self.cars.append(c[i])
else:
print('Error: cannot update the list of cars')
break
def get_inhabitants(self):
inhab_names =[n.name for n in self.inhabitants]
inhab_ages =[n.age for n in self.inhabitants]
return {n:a for (n,a) in zip(inhab_names,inhab_ages)}
def get_cars(self):
car_makes =[c.make for c in self.cars]
car_models =[c.model for c in self.cars]
return [[x,y] for x, y in zip(car_makes,car_models)]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!