Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a function add _ owners that will match cars with their owners based on a dictionary passed as a parameter. The dictionary uses owners
Write a function addowners that will match cars with their owners based on a dictionary passed as a parameter. The dictionary uses owners Person objects as keys and tuples of Car objects they own, as values. You mayshould reuse the previously developed function getaddress to retrieve Persons addresses. Note that this function does not print or return anything, its role is to modify existing objects.
Example usage:
houses
h House Happy St Blackwood'
h House Agnes Av Aldinga'
h House Daw St Aldinga'
h House Oak St Blackwood'
h House Gorge Av Aldinga'
h House Waterman St Aldinga'
c CarFord'Mustang',
c CarFord'Fiesta',
c CarToyota'Camry',
c CarMazdaCx
c CarMazdaCx
c CarMitsubishi'Colt',
c CarToyota'Corolla',
p PersonJim
p PersonAshley
p PersonCleo
p PersonJessica
p PersonAdam
p PersonAndrew
p PersonJake
p PersonJoshua
p PersonAmica
p PersonHelen
p PersonBarney
p PersonSandra
p PersonJohn
p PersonPatrick
p PersonSammy
haddcarcc
haddcarc
haddcarc
haddcarccc
haddcarc
haddcarcc
haddinhabitantpppp
haddinhabitantpp
haddinhabitantppp
haddinhabitantp
haddinhabitantpp
haddinhabitantppp
houses.extendhhhhhh
d p:ccp:ccp:ccc p:c
addownershousesd
printcgetownerslist
printcgetownerslist
printcgetownerslist
output:
Jim Happy St Blackwood'Cleo Happy St Blackwood'
Ashley Happy St Blackwood'
Adam Agnes Av Aldinga'
Here are the Classes:
class Person:
def initself name, age:
self.name name
self.age age
def strself:
return 'This is self.name strselfage years old.
def isteenagerself:
return self.age
class Car:
def initself make,model,age:
self.make make
self.model model
self.age age
self.owners
def strself:
return 'This is: self.make strselfage years old.
def addownerselfo:
for i in rangeleno:
if isinstanceoiPerson and isinstanceoistr:
self.ownersoi oi
else:
printError: cannot update the list of car owners'
break
def getownerslistself:
olist nname for n in self.owners.keys
return xy for xy in zipolist,self.owners.values
class House:
def initself address:
self.address address
self.inhabitants
self.cars
def strself:
return 'This house is inhabited by strlenselfinhabitants people.
def addinhabitantselfp:
for i in rangelenp:
if isinstancepiPerson:
self.inhabitants.appendpi
else:
printError: cannot update the list of inhabitants'
break
def addcarselfc:
for i in rangelenc:
if isinstanceciCar:
self.cars.appendci
else:
printError: cannot update the list of cars'
break
def getinhabitantsself:
inhabnames nname for n in self.inhabitants
inhabages nage for n in self.inhabitants
return n:a for na in zipinhabnames,inhabages
def getcarsself:
carmakes cmake for c in self.cars
carmodels cmodel for c in self.cars
return xy for x y in zipcarmakes,carmodels
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started