Question
The program of Exercise P8.17 is not very user-friendly because it requires the user to know the exact spelling of the country name. As an
The program of Exercise P8.17 is not very user-friendly because it requires the user to know the exact spelling of the country name. As an enhancement, whenever a user enters a single letter, print all countries that start with that letter. Use a dictionary whose keys are letters and whose values are sets of country names.
Use the rawdata_2004.txt file, provided below, to load your dictionary.
Can you please solve this question by the following template in python?
# Read a database of per capita income by country and allow the user to # query it. # # Load the data into a dictionary. incomes = {} inf = open("rawdata_2004.txt", "r") for line in inf: parts = line.split("\t") # Remove the dollar sign and comma. parts[2] = parts[2].replace("$", "") parts[2] = parts[2].replace(",", "") # Add the country to the dictionary. incomes[parts[1].upper()] = float(parts[2]) # Read queries from the user and respond to them. country = input("Enter a country name (or type quit to quit): ").upper() while country != "QUIT" : if country in incomes : print("The per capita income is", incomes[country]) else : print("That wasn't a recognized country.") print() # Read the next country from the user. country = input("Enter a country name (or type quit to quit): ").upper()
Here the key is a country name and the value is income.
But I want the key is letter and value is all country's name starting with same letter with per capita income by using same rawdata_2004.txt
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started