so this code is suppose to output all the data that goes with each town name, but its not outputting anything and im a little lost.
This is what i have:
def calculate_average_height(height_data):
total_height = sum(height_data)
average_height = total_height / len(height_data)
return average_height
def find_closest_height(height_data, average_height):
closest_height = min(height_data, key=lambda h: abs(h - average_height))
return closest_height
def find_second_closest_height(height_data, closest_height):
second_closest_height = min([h for h in height_data if h != closest_height], key=lambda h: abs(h - average_height))
return second_closest_height
def main()
city_data = [
{"city": 'Ely', 'Population”': 10, 'Height': [67, 66, 68, 62, 65, 64, 84, 71, 62, 81], "names": ['Mariam Cox', 'Mikayla Bonner', 'Jaquelyn Bowman', 'Henry Dickson', 'Daniel Ashley', 'Michelle Wilkerson', 'Shannon Hogan', 'Justine Carrillo', 'Dustin Lara', 'Gabriel Graham']},
{"city": 'Grand Marais', 'Population': 11, 'Height': [75, 81, 62, 67, 83, 63, 76, 65, 68, 69, 65],'names': ['Oliver Clay', 'Rae Finley', 'John Fuentes', 'Macy Baxter', 'Quentin Campbell', 'Kareem Cummings','Keane Mckenzie', 'Marah Kelley', 'Scott Chambers', 'Oren Gilmore', 'Colton Osborn']},
{"city": 'Finland', 'Population': 15, 'Height': [68, 80, 64, 81, 81, 83, 62, 72, 68, 62, 72, 66, 69, 71, 63],'names': ['Brady Joyce', 'Harding Jones', 'Zeph Dorsey', 'Phelan Good', 'Kai Mcfarland', 'Giacomo Goodman','Nigel Pena', 'Basil Lawrence', 'Julian Reynolds', 'Alexis Compton', 'Paki Franklin','Clio Valencia','Simon Holt', 'Griffith Watson', 'Maxine Graves']},
{"city": 'Knife River', 'Population': 11, 'Height': [64, 81, 68, 65, 74, 73, 82, 77, 79, 76, 76],'names': ['Marvin Powell', 'Clio Henry', 'Hall Mason', 'Genevieve Gardner', 'Audra Watkins', 'Eagan Garza','Brock Bolton', 'Mufutau Harrington', 'Gannon Shaw', 'Amery Orr', 'Lane Diaz']},
{"city": 'Lutsen', 'Population': 14, 'Height': [70, 79, 75, 67, 66, 65, 69, 78, 76, 61, 67, 79, 67, 77], 'names': ['Kelly Washington', 'Charity Moran', 'Dustin Wall', 'September Mcgowan', 'Deirdre Weaver', 'Audra Hopkins', 'Roanna Tyson', 'Winter Herrera', 'Desiree Ramsey', 'Maryam West', 'Xena Hewitt', 'Lester Tanner', 'Lesley Contreras', 'Rashad Hartman']},
{"city": 'Grand Portage', 'Population': 11, 'Height': [70, 73, 71, 84, 66, 82, 79, 60, 64, 77, 78], 'names': ['Hanna Carr', 'Vivien Alexander', 'Lareina Sims', 'Shelley Compton', 'Alec Pratt', 'Kirestin Torres','Isaiah Brewer', 'Buffy Hewitt', 'Ryder Bean', 'Cherokee Freeman', 'Victoria Watkins']},
{"city": 'Two Harbors', 'Population': 6, 'Height': [66, 77, 82, 63, 84, 75], 'names': ['Blake Clayton', 'Rinah Durham', 'Chiquita King', 'Cathleen Beard', 'Camilla Preston','Gloria Waller']},
{'city': 'Silver Bay', 'Population': 8, 'Height': [83, 68, 82, 71, 76, 78, 78, 66],
'names': ['Lucius Henson', 'Madeline Baird', 'Amos Chang', 'Avye Chandler', 'Sarah Chang', 'Marah Mclean','Brady Houston', 'Britanney Sandoval']},
{"city": 'Hovland', 'Population': 4, 'Height': [76, 83, 66, 74], 'names': ['Jack Guntrie', 'Burke Whitney', 'Molly Mclaughlin', 'Joel George']},
{"city": 'Beaver Bay', 'Population': 10, 'Height': [69, 69, 68, 68, 69, 70, 78, 74, 80, 75], 'names': ['Igor Moore', 'Caldwell Bullock', 'Ralph Sanchez', 'Abraham Aquirre', 'Odysseus Alston', 'Aphrodite Mays', 'Raymond Clark', 'Ivor Melton', 'Lareina Hickman', 'Mackensie Burch']}]
for city in city_data:
average_height = calculate_average_height(city["Height"])
closest_height = find_closest_height(city["Height"], average_height)
second_closest_height = find_second_closest_height(city["Height"], closest_height)
# Find corresponding person names (similar to the original code)
print(f"{city['city']} ({city['Population']} people):")
print(f"Average height: {average_height:.2f} cm")
print(f"Closest height to average: {closest_height} cm (Person: {closest_person_name})")
print(f"Second closest height to average: {second_closest_height} cm (Person: {second_closest_person_name})")
print() # Add a newline for readability
if __name__ == "__main__":
main()
I have tweaked it repeatedly but nothing helped and i didnt really know where to go from there. i would like the output to contain all cities along with the other functions i created.
New contributor
ltrow2017 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.