PYTHON PROGRAM Not too sure how to go about writting this program please help!!
ID: 3735989 • Letter: P
Question
PYTHON PROGRAM
Not too sure how to go about writting this program please help!!
The input file looks as follows:
1:Qatar:98900
2:Liechtenstein:89400
3:Luxembourg:80600
4:Bermuda:69900
5:Singapore:59700
6:Jersey:57000
...
and so on all the way to #228.
This is what I need to learn to do:
For this exercise, you will write a program that makes use of two dictionaries. The program
should read the data in rawdata.txt into a dictionary whose keys are country names and whose
values are per capita incomes. Also create another dictionary whose keys are letters A Z and
whose values are sets of country names. Convert the country names to all uppercase for both
dictionaries.
Your program should prompt the user to enter an initial or a country names and then print the
corresponding values.
If the user enters a string with only one letter (like A), the program should print all the
countries that begin with that letter,
If the user enters a string that contains more than one letter, print the corresponding
value for that country if it exists or print “Does not exist” if invalid.
Stop when the user enters quit.
Explanation / Answer
income={} country={} with open('rawdata.txt','r') as f: for line in f: index,n,v=line.split(':') name=n.upper() value=int(v) income[name]=value if name[0] in country: country[name[0]].append(name) else: country[name[0]]=[name] a='a' while a.lower()!='quit': a=input('Enter an initial or a country name: ') if a.lower()!='quit': if len(a)==1: if a.upper() in country: print(country[a.upper()]) else: print('Does not exist') else: if a.upper() in income: print(income[a.upper()]) else: print('Does not exist')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.