192k views
3 votes
Write a loop that prints each country's population in country_pop. Sample output for the given program.

United States has 318463000 people.
India has 1247220000 people.
Indonesia has 252164800 people.
China has 1365830000 people.
country_pop = {
'China': 1365830000,
'India': 1247220000,
'United States': 318463000,
'Indonesia': 252164800
} # country populations as of 2014
print(country, 'has', pop, 'people.')

User Erenon
by
5.4k points

1 Answer

3 votes

Answer:

Correct code for the above question which is written after the defined list to display the output.

for x,y in country_pop.items(): #for loop to prints the list items.

print(str(x)+" has "+str(y)+" people") #print function to print the value.

Output:

  • The above code is in python language which display the output as the above question demands.

Step-by-step explanation:

  • The above question code is written in python language to display the result, but this code is not correct.
  • The above code needs a 'for' loop, which will render the list item one by one.
  • The above-list defined in the form of key-value pair, So there is a need for the ''item" function which is the part of the python dictionary to display the list items.
  • There also needs to define the two variables in the for loop, one is for key and the other is for value.
User MatheusOl
by
4.9k points