Question :1 Write a Python program that reads the data from the attached pipe-de
ID: 3851767 • Letter: Q
Question
Question :1
Write a Python program that reads the data from the attached pipe-delimited world_gdp_data.txt file into a dictionary whose keys are country names and whose values are the corresponding per capita GDP values. Then the program should prompt the user to enter country names and display the corresponding GDP values until the user enters quit.
world_gdp_data.txt file :- https://mega.nz/#!A7hXxQwT!B6PhiIz4uHsKJaoa-U_il3427m_x1nAQaqoSRHkCga8
Sol:- https://mega.nz/#!NuQ0VBgD!wxivgDNOcKoace3bgQgrnaqLczMMFgjk0-hjYmNpFek
Modify the Above program to make it more user-friendly. Since the exact spelling of country names can be difficult to remember, enhance the program to allow the user to enter a single character at the prompt to display a list of countries whose names start with that letter. Use a dictionary whose keys are letters and whose values are sets of country names that begin with a given letter.
Explanation / Answer
NOTE: I wrote two programs, one for the first version and another for the modified program requirement. Please check and let me know if you face any issues. I will revert back within 24 hours.
Also to easily copy the code i have shared below code in the following links.
code.1: http://pasted.co/d88c5e1d
code.2: http://pasted.co/042861c4
Code.1:
#!/usr/local/bin/python3
def main():
# opening a file in read mode
with open("world_gdp_data.txt") as fp:
country_gdp = {}
# fetching each line from file and storing in hash country_gdp
for line in fp:
country, gdp = line.split('|')
country_gdp[country] = gdp
fp.close()
# getting the country name from user as input
contry = input('Enter country name: ')
# checking if country gdp details exist with us
if contry in country_gdp:
print('GDP for the country', contry, 'is',country_gdp[contry])
else:
print('Given country', contry, 'does not exist in our database')
if __name__=='__main__':
main()
Execution output screenshot:
https://pasteboard.co/GA8Zk0V.png
Code.2:
#!/usr/local/bin/python3
def main():
# opening a file in read mode
with open("world_gdp_data.txt") as fp:
country_gdp = {}
countries = {}
# fetching each line from file and storing in dictionary country_gdp
for line in fp:
country, gdp = line.split('|')
country_gdp[country] = gdp
# fetching first letter from country name and storing in countries dictionary
first_letter = country[0]
if first_letter in countries:
countries[first_letter].append(country)
else:
countries[first_letter] = [country]
fp.close()
fletter = input('Enter the first letter of country name: ')
if fletter in countries:
print('countries starting with letter', fletter, 'are:')
print(countries[fletter])
# getting the country name from user as input
contry = input('Enter country name in the above list: ')
# checking if country gdp details exist with us
if contry in country_gdp:
print('GDP for the country', contry, 'is',country_gdp[contry])
else:
print('Given country', contry, 'does not exist in our database')
else:
print('No countries available in our database with the starting letter', fletter)
if __name__=='__main__':
main()
Execution output screenshot:
https://pasteboard.co/GA8ZWFu.png
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.