Question: import csv from operator import itemgetter # Lists of valid personalities and games personalities = [ shy , courageous, aggressive, calm, needy, curious,
import csv
from operator import itemgetter
# Lists of valid personalities and games
personalities shy "courageous", "aggressive", "calm", "needy", "curious", "independent"
games swim "platforms", "handfeeding", "puzzles", "fake hunting", "swings", "hiding"
# Function to read the species file and create a species dictionary
def readspeciesfilefilename:
try:
speciesdict
f openfilenamer
reader csvreaderf
for linenumber, row in enumeratereader start:
speciesdictlinenumber row # species ID is the line number
fclose # Make sure to close the file after reading
return speciesdict
except FileNotFoundError:
printfError File filename does not exist"
return None
# Function to read the expenses file and create an expense dictionary
def readexpensesfilefilename:
try:
expensesdict
f openfilenamer
reader csvreaderf
for row in reader:
item, expense, rate row floatrow row
if rate "peryear":
expensesdictitem expense
elif rate "perday":
expensesdictitem expense
else:
expensesdictitem expense # per month
fclose # Make sure to close the file after reading
return expensesdict
except FileNotFoundError:
printfError File filename does not exist"
return None
# Function to read the zoo file and create a list of animal data
def calculatesickpercentagezoos:
results
for zoo in sortedzooskeys:
totalanimals sumlenanimals for animals in zooszoovalues
sickanimals sum for species in zooszoovalues for animal in species if animalhealth 'sick'
percentagesick sickanimals totalanimals if totalanimals else
resultszoo
'percentage': intpercentagesick
'sickspecies': findsickestspecieszooszoo
printresultsresults
def findsickestspeciesspeciesdata:
sickcount
for species, animals in speciesdata.items:
sickcountspecies sum for animal in animals if animalhealth 'sick'
maxsick maxsickcount.values
return sortedspecies for species, count in sickcount.items if count maxsick
def printresultsresults:
for zoo, data in sortedresultsitems:
print
fdatapercentage of zootitle Zoo animals are sick. The sickest being joindatasickspecies'
healthiestzoo determinehealthiestzooresults
printfhealthiestzoo Zoo takes better care of its animals!!!"
def determinehealthiestzooresults:
healthiest minresultsitems keylambda x: xpercentage
return healthiesttitle
def calculatemonthlyexpenseszoos:
expenses
for zoo in sortedzooskeys:
totalexpense sumanimalmonthlyexpense' for animal in zooszooanimals
expenseszoo totalexpense
printexpenseresultsexpenses
def printexpenseresultsexpenses:
for zoo, cost in sortedexpensesitems:
printfzootitle Zoo spends $cost:f a month."
mostexpensive determinemostexpensivezooexpenses
printfmostexpensive Zoo is the most expensive zoo!!!"
def determinemostexpensivezooexpenses:
maxcost maxexpensesvalues
mostexpensivezoos sortedzoo for zoo, cost in expenses.items if cost maxcost
return and joinmostexpensivezoos
def getanimaldetailszoos:
while True:
zoo inputEnter a zoo:
if zoo not in zoos:
printInvalid zoo or does not exist."
continue
species inputEnter an animal species:
if species not in zooszoo:
printInvalid species or does not exist."
continue
name inputEnter the animal's name:
animal nexta for a in zooszoospecies if aname name None
if not animal:
printInvalid name or does not exist."
continue
printanimaldetailsanimal
break
def printanimaldetailsanimal:
print
fanimalname
Species ID: animalspeciesid
Age: animalage years old
Sex: animalsex
Health: animalhealth
Years at Zoo: animal
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
