Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The program of Exercise P 8 . 1 7 is not very user - friendly because it requires the user to know the exact spelling
The program of Exercise P is not very userfriendly 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 python, Keep the original program solution to Exercise P up to the creation of the incomes dictionary with the key country name and values per capita income Create a new dictionary letters with the key alphabet letter and values a set of the country names that start with the letter in the key Follow the steps below to create this dictionary:
iterate over the keys country names in the first dictionary incomes and if the first letter of the key country name is not a key in the new dictionary letters
add a new entry to the dictionary: key: first letter of the country name,
value: an empty set and add the country name to the set, indexed by the first letter of the country name as the key
Modify the part for reading queries from the user and responding in the original program.
Follow the steps below for the user input and the program response:
Prompt the user for input
if the text entered by the user is of length :
use the new dictionary letters to list the countries starting with that letter
else use the text entered by the user to look for the country name in the first
dictionary incomes and
if found print the per capita income
else print the text entered is not a recognized country
Here's what i have so far in python:
incomes
inf openrawdatatxtr
for line in inf:
parts line.splitt
# Remove the dollar sign and comma.
parts partsreplace$
parts partsreplace
# Add the country to the dictionary.
incomespartsupper floatparts
# Read queries from the user and respond to them.
country inputEnter a country name or type quit to quit: upper
while country "QUIT" :
if country in incomes :
printThe per capita income is incomescountry
else :
printThat wasn't a recognized country."
print
# Read the next country from the user.
country inputEnter a country name or type quit to quit: upper
letters
for country in incomes.keys:
firstletter country
if firstletter not in letters:
lettersfirstletter set
lettersfirstletteraddcountry
# Modified part for reading queries from the user and responding
while True:
userinput inputEnter a country name or the first letter of a country:
if lenuserinput: # If the user enters a single letter
letter userinput.upper
if letter in letters:
countrieswithletter joinsortedlettersletter
printfCountries starting with letter: countrieswithletter
else:
printfNo countries found starting with letter
else: # If the user enters a full country name
countryname userinput.capitalize
if countryname in incomes:
printfThe per capita income of countryname is incomescountryname
else:
printfThe entered text userinput is not a recognized country."
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