Question: 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 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 python, Keep the original program (solution to Exercise P8.17) 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 ==1:
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 = open("rawdata_2004-1.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()
letters ={}
for country in incomes.keys():
first_letter = country[0]
if first_letter not in letters:
letters[first_letter]= set()
letters[first_letter].add(country)
# Modified part for reading queries from the user and responding
while True:
user_input = input("Enter a country name or the first letter of a country: ")
if len(user_input)==1: # If the user enters a single letter
letter = user_input.upper()
if letter in letters:
countries_with_letter =','.join(sorted(letters[letter]))
print(f"Countries starting with '{letter}': {countries_with_letter}")
else:
print(f"No countries found starting with '{letter}'")
else: # If the user enters a full country name
country_name = user_input.capitalize()
if country_name in incomes:
print(f"The per capita income of {country_name} is {incomes[country_name]}")
else:
print(f"The entered text '{user_input}' is not a recognized country.")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!