Write a function sum.populations ) that takes five arguments, in this order: l.
ID: 3741005 • Letter: W
Question
Write a function sum.populations ) that takes five arguments, in this order: l. which-continent: one of the following strings: , Africa, , , Americas, , , Asia , , ?? rope, or Oceania' 2. min.gdp: a positive integer 3 countries: a dictionary that maps a continent name to a list of some of the countries in that continent 4. gdps: a dictionary that maps a country name to that country's GDP (gross domestic product) in millions USD (SMM) 5. populations: a dictionary that maps a country name to its populationExplanation / Answer
Python Executable Code :-
def sum_populations(which_continent,min_gdp,countries,gdps,populations):
all_contries_in_which_continent=countries[which_continent] #all_contries_in_which_continent will store all the countries which are in which_continent continent
populationCount=0 #initialising populationCount with zero
for country in all_contries_in_which_continent: #loop it for all the country in which_continent
if gdps[country] >= min_gdp: # if gdp of that country is atleast min_gdp then add population of that country to populationCount
populationCount+=populations[country] #adding population of country here
return populationCount #returning the populationCount
countries = { 'Americas': ['Argentina', 'Uruguay', 'Brazil', 'Puerto Rico', 'Panama'], 'Oceania': ['Fiji', 'Australia', 'New Zealand', 'Papua New Guinea'], 'Asia': ['Myanmar', 'Israel'], 'Africa': ['Senegal', 'Lesotho', 'South Africa', 'Togo', 'Sierra Leone'], 'Europe': ['Norway', 'Greece']}
gdps = { 'Argentina': 545866, 'Uruguay': 52420, 'Brazil': 1796186, 'Puerto Rico': 103135, 'Panama': 55188, 'Fiji': 4632, 'Australia': 1204616, 'New Zealand': 185017, 'Papua New Guinea': 16929, 'Myanmar': 67430, 'Israel': 318744, 'Senegal': 14765, 'Lesotho': 2200, 'South Africa': 294841, 'Togo': 4400, 'Sierra Leone': 3669, 'Norway': 370557, 'Greece': 1945591 }
populations = {'Argentina': 43847430, 'Uruguay': 3444006, 'Brazil': 207652865, 'Puerto Rico': 3667903, 'Panama': 4034119, 'Fiji': 898760, 'Australia': 24125848, 'New Zealand': 4660833, 'Papua New Guinea': 8084991, 'Myanmar': 52885223, 'Israel': 8191828, 'Senegal': 15411614, 'Lesotho': 2203821, 'South Africa': 56015473, 'Togo': 7606374, 'Sierra Leone': 7396190, 'Norway': 5254694, 'Greece': 11183716}
print "TOTAL POPULATION = ",sum_populations('Americas',1000000,countries,gdps,populations) #calling the function
SAMPLE OUTPUT :-
CONTINENT NAME is Americas
MINIMUM GDP = 1000000
CONTRIES WHICH HAVE ATLEAST 1000000 IN THE CONTINENT Americas IS/ARE
Brazil
TOTAL POPULATION = 207652865
***********************************************
CONTINENT NAME is Oceania
MINIMUM GDP = 1000000
CONTRIES WHICH HAVE ATLEAST 1000000 IN THE CONTINENT Oceania IS/ARE
Australia
TOTAL POPULATION = 24125848
***********************************************
CONTINENT NAME is Asia
MINIMUM GDP = 1000000
CONTRIES WHICH HAVE ATLEAST 1000000 IN THE CONTINENT Asia IS/ARE
TOTAL POPULATION = 0
***********************************************
CONTINENT NAME is Asia
MINIMUM GDP = 300000
CONTRIES WHICH HAVE ATLEAST 300000 IN THE CONTINENT Asia IS/ARE
Israel
TOTAL POPULATION = 8191828
***********************************************
CONTINENT NAME is Africa
MINIMUM GDP = 1000000
CONTRIES WHICH HAVE ATLEAST 1000000 IN THE CONTINENT Africa IS/ARE
TOTAL POPULATION = 0
***********************************************
CONTINENT NAME is Africa
MINIMUM GDP = 100000
CONTRIES WHICH HAVE ATLEAST 100000 IN THE CONTINENT Africa IS/ARE
South Africa
TOTAL POPULATION = 56015473
***********************************************
Python Code Which Stores Result In A File (out.txt) :-
f=open('out.txt','a') #create one empty file in your current directory with name out.txt
def sum_populations(which_continent,min_gdp,countries,gdps,populations):
global f#global variable f accessing inside function
f.write("CONTINENT NAME is "+which_continent+' '+'MINIMUM GDP = '+str(min_gdp)+' ')
f.write("CONTRIES WHICH HAVE ATLEAST "+str(min_gdp)+" IN THE CONTINENT "+which_continent+' IS/ARE ')
all_contries_in_which_continent=countries[which_continent] #all_contries_in_which_continent will store all the countries which are in which_continent continent
populationCount=0 #initialising populationCount with zero
for country in all_contries_in_which_continent: #loop it for all the country in which_continent
if gdps[country] >= min_gdp: # if gdp of that country is atleast min_gdp then add population of that country to populationCount
f.write(country+' ')
populationCount+=populations[country] #adding population of country here
f.write(" TOTAL POPULATION = "+str(populationCount)+' ')
f.write(" *********************************************** ")
return populationCount #returning the populationCount
countries = { 'Americas': ['Argentina', 'Uruguay', 'Brazil', 'Puerto Rico', 'Panama'], 'Oceania': ['Fiji', 'Australia', 'New Zealand', 'Papua New Guinea'], 'Asia': ['Myanmar', 'Israel'], 'Africa': ['Senegal', 'Lesotho', 'South Africa', 'Togo', 'Sierra Leone'], 'Europe': ['Norway', 'Greece']}
gdps = { 'Argentina': 545866, 'Uruguay': 52420, 'Brazil': 1796186, 'Puerto Rico': 103135, 'Panama': 55188, 'Fiji': 4632, 'Australia': 1204616, 'New Zealand': 185017, 'Papua New Guinea': 16929, 'Myanmar': 67430, 'Israel': 318744, 'Senegal': 14765, 'Lesotho': 2200, 'South Africa': 294841, 'Togo': 4400, 'Sierra Leone': 3669, 'Norway': 370557, 'Greece': 1945591 }
populations = {'Argentina': 43847430, 'Uruguay': 3444006, 'Brazil': 207652865, 'Puerto Rico': 3667903, 'Panama': 4034119, 'Fiji': 898760, 'Australia': 24125848, 'New Zealand': 4660833, 'Papua New Guinea': 8084991, 'Myanmar': 52885223, 'Israel': 8191828, 'Senegal': 15411614, 'Lesotho': 2203821, 'South Africa': 56015473, 'Togo': 7606374, 'Sierra Leone': 7396190, 'Norway': 5254694, 'Greece': 11183716}
print "TOTAL POPULATION = ",sum_populations('Africa',100000,countries,gdps,populations) #calling the function
If You Found the answers useful please give thums up(really helpful).
Thanks.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.