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 read_species_file(filename):
try:
species_dict ={}
f = open(filename,'r')
reader = csv.reader(f)
for line_number, row in enumerate(reader, start=1):
species_dict[line_number]= row[0] # species ID is the line number
f.close() # Make sure to close the file after reading
return species_dict
except FileNotFoundError:
print(f"Error. File '{filename}' does not exist")
return None
# Function to read the expenses file and create an expense dictionary
def read_expenses_file(filename):
try:
expenses_dict ={}
f = open(filename,'r')
reader = csv.reader(f)
for row in reader:
item, expense, rate = row[0], float(row[1]), row[2]
if rate == "peryear":
expenses_dict[item]= expense /12
elif rate == "perday":
expenses_dict[item]= expense *30
else:
expenses_dict[item]= expense # per month
f.close() # Make sure to close the file after reading
return expenses_dict
except FileNotFoundError:
print(f"Error. File '{filename}' does not exist")
return None
# Function to read the zoo file and create a list of animal data
def calculate_sick_percentage(zoos):
results ={}
for zoo in sorted(zoos.keys()):
total_animals = sum(len(animals) for animals in zoos[zoo].values())
sick_animals = sum(1 for species in zoos[zoo].values() for animal in species if animal['health']== 'sick')
percentage_sick =(sick_animals / total_animals)*100 if total_animals >0 else 0
results[zoo]={
'percentage': int(percentage_sick),
'sick_species': find_sickest_species(zoos[zoo])
}
print_results(results)
def find_sickest_species(species_data):
sick_count ={}
for species, animals in species_data.items():
sick_count[species]= sum(1 for animal in animals if animal['health']== 'sick')
max_sick = max(sick_count.values())
return sorted([species for species, count in sick_count.items() if count == max_sick])
def print_results(results):
for zoo, data in sorted(results.items()):
print(
f"{data['percentage']}% of {zoo.title()} Zoo animals are sick. The sickest being {','.join(data['sick_species'])}.")
healthiest_zoo = determine_healthiest_zoo(results)
print(f"{healthiest_zoo} Zoo takes better care of its animals!!!")
def determine_healthiest_zoo(results):
healthiest = min(results.items(), key=lambda x: x[1]['percentage'])
return healthiest[0].title()
def calculate_monthly_expenses(zoos):
expenses ={}
for zoo in sorted(zoos.keys()):
total_expense = sum(animal['monthly_expense'] for animal in zoos[zoo]['animals'])
expenses[zoo]= total_expense
print_expense_results(expenses)
def print_expense_results(expenses):
for zoo, cost in sorted(expenses.items()):
print(f"{zoo.title()} Zoo spends ${cost:,.2f} a month.")
most_expensive = determine_most_expensive_zoo(expenses)
print(f"{most_expensive} Zoo is the most expensive zoo!!!")
def determine_most_expensive_zoo(expenses):
max_cost = max(expenses.values())
most_expensive_zoos = sorted([zoo for zoo, cost in expenses.items() if cost == max_cost])
return ' and '.join(most_expensive_zoos)
def get_animal_details(zoos):
while True:
zoo = input("Enter a zoo: ")
if zoo not in zoos:
print("Invalid zoo or does not exist.")
continue
species = input("Enter an animal species: ")
if species not in zoos[zoo]:
print("Invalid species or does not exist.")
continue
name = input("Enter the animal's name: ")
animal = next((a for a in zoos[zoo][species] if a['name']== name), None)
if not animal:
print("Invalid name or does not exist.")
continue
print_animal_details(animal)
break
def print_animal_details(animal):
print(
f"{animal['name']}
Species ID: {animal['species_id']}
Age: {animal['age']} years old
Sex: {animal['sex']}
Health: {animal['health']}
Years at Zoo: {animal

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 Programming Questions!