Ask Question
10 August, 14:17

Write a loop that prints each country's population in country_pop. Sample output with input: 'China:1365830000, India:1247220000, United States:318463000, Indonesia:252164800': United States has 318463000 people. India has 1247220000 people.

+5
Answers (1)
  1. 10 August, 17:32
    0
    Hi there! This question is good to check your understanding of loops. To illustrate, I have implemented the answer in Python and explained it below.

    Explanation:

    Assuming user input for the country and population list as provided in the question, we can write the python script "country_pop. py" and run it from the command line. First we prompt user for the input, after which we split the country-population list which is separated or delimited with a comma into an array of country-population list. We then create a hash of country and its corresponding population by further splitting the country-population combo into a hash, and finally, printing the resultant hash as the output.

    country_pop. py

    def add_country_pop (country, population):

    countrypop_hash[country]=population

    countrypop_hash = {}

    input_string = input ("Enter country population list: ")

    print (input_string)

    countrypop_array = input_string. split (",")

    for country in countrypop_array:

    country_pop = country. split (':')

    pop_country = country_pop[0]

    population = country_pop[1]

    add_country_pop (pop_country, population)

    print (country + ' has ' + population + ' people')

    Output

    > country_pop. py

    Enter country population list: China:1365830000, India:1247220000, United States:318463000, Indonesia:252164800

    China:1365830000, India:1247220000, United States:318463000, Indonesia:252164800

    China:1365830000 has 1365830000 people

    India:1247220000 has 1247220000 people

    United States:318463000 has 318463000 people

    Indonesia:252164800 has 252164800 people
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a loop that prints each country's population in country_pop. Sample output with input: 'China:1365830000, India:1247220000, United ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers