Coding Problem 4.5.7 (External resource) (3.0 points possible) PopulationDensity
ID: 3704190 • Letter: C
Question
Coding Problem 4.5.7 (External resource) (3.0 points possible) PopulationDensity.py Submit Run Grades Reset 1 # rite a function called population-density. The function Console output will be displayed here 2 #should take one parameter, which will be a list of 3 #dictionaries. Each dictionary in the list will have three 4 #key-value pairs; 5# 6 # -name: the name of the country 7 # -population: the population of that country 8 # -area: the area of that country (in km^2) 9# 10 #Your function should return the population density of all 11 #the countries put together. You can calculate this by 12 #summing all the populations, summing all the areas, and 13 #dividing the total population by the total area 14# 15 #Note that the input to this function will look quite long; 16 #don't let that scare you. That's just because dictionaries 17 #take a lot of text to define 19 20 #Add your function here! 21 23 24 #Below are some lines of code that will 25 #You can change the value of the variable(s) to test your 26 #function with different inputs 27 # test your function 28 #If your function works correctly, this will originally 29 #print: 133.886 (or something around there 30 countries nameChina", "population": 1399708000, "area: 9640821), 31 32 name: "India", "population": 134800300, area: 3287248 name "United States", "population": 325300000, "area": 9826675, name: "Indonesia", "population": 237556363 "area": 19045691 34 print (population_density(countries)) 35 36 37 38Explanation / Answer
def population_density(countries): total_pop = 0 total_area = 0 for d in countries: total_area += d['area'] total_pop += d['population'] return total_pop / total_area
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.