Answered step by step
Verified Expert Solution
Link Copied!

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 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Vi Special Issue On Database And Expert Systems Applications Lncs 7600

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2012th Edition

3642341780, 978-3642341786

More Books

Students also viewed these Databases questions

Question

What stage of development is your industry in?

Answered: 1 week ago