subject

Now add code to the function read_gdp_data() so that it looks up the happiness index for each country in the dictionary. If the country does not exist in the dictionary, that country should be skipped. Finally add a fourth column to your output that includes that happiness index formatted to two decimal places of precision. The first five lines of your output should look like this: Country GDP per Capita Population in Millions Happiness
China 18192.04 1394.02 5.13
United States 58592.03 332.64 6.88
India 7144.29 1326.09 3.82
Japan 43367.94 125.51 5.79
For this assignment, you do not have to write your output to a file.
When you are satisfied your program works, save and submit
happy2 is :
import csv
# function to read a tsv(tab-separared-values) file and display the re-formatted contents after removing the commas and dollar signs
def read_gdp_data():
valid_characters = '0123456789.' # string of valid characters
filename = 'world_pop_gdp. tsv' # input filename
# open the file in read mode
with open(filename) as fp:
contents = fp. readlines() # read the contents of the file into the list with each line being an element of the list
headers = contents[0].strip().split('\t') # split the first record in list contents using tab(\t) as the delimiter
# display the header
print('%-s\t%-s\t%-s'%(headers[0],h eaders[2],headers[1]))
# loop over the contents file from index 1 to end of file
for i in range(1,len(contents)):
data = contents[i].split('\t') # split the record in contents[i] using tab(\t) as the delimiter
country = data[0].strip() # extract the country information
pop = data[1].strip() # extract the population information
gdp = data[2].strip() # extract the gdp information
# loop to modify the population information so that it removes commas
mod_pop = ''
for ch in pop:
if ch in valid_characters:
mod_pop = mod_pop + ch
# loop to modify the gdp information so that it removes commas and dollar sign
mod_gdp = ''
for ch in gdp:
if ch in valid_characters:
mod_gdp = mod_gdp + ch
# display the formatted information
print('%-s\t%-s\t%-s'%(country, mod_gdp, mod_pop))
fp. close() # close the file
def make_happy_dict():
filename = 'happiness. csv' # update the file path/name here
happy_dict={}
with open(filename, 'r') as infile:
csvreader = csv. reader(infile)
for line in csvreader:
happy_dict[line[0]]=float(line[2])< br /> return happy_dict
def print_sorted_dictionary(D):
for key in sorted(D. keys()):
print(key, D[key])
def main():
# Build dictionary mapping countries to happiness index
#happy_dict = make_happy_dict()
#print_sorted_dictionary(happy_dict )
read_gdp_data() # call the read_gdp_data function
# call the main function
main()
#end of program

Data

Afghanistan,2018,2.694303274
Albania,2018,5.004402637
Algeria,2018,5.043086052
Angola,2014,3.794837952
Argentina,2018,5.792796612
Armenia,2018,5.062448502
Australia,2018,7.17699337
Austria,2018,7.396001816
Azerbaijan,2018,5.167995453
Bahrain,2017,6.227320671
Bangladesh,2018,4.499217033
Belarus,2018,5.233769894
Belgium,2018,6.89217186
Belize,2014,5.955646515
Benin,2018,5.81982708
Bhutan,2015,5.082128525
Bolivia,2018,5.915734291
Bosnia and Herzegovina,2018,5.887401104
Botswana,2018,3.4613657
Brazil,2018,6.190921783
Bulgaria,2018,5.098813534
Burkina Faso,2018,4.92723608
Burundi,2018,3.775283098

Data:

Country Population in Millions GDP per Capita
China 1,394.02 $18,192.04
United States 332.64 $58,592.03
India 1,326.09 $7,144.29
Japan 125.51 $43,367.94
Germany 80.16 $52,382.96
Russia 141.72 $28,337.13
Indonesia 267.03 $12,171.08
Brazil 211.72 $15,341.31
United Kingdom 65.76 $44,479.17
France 67.85 $42,094.00
Mexico 128.65 $19,145.03
Italy 62.40 $37,129.83
Turkey 82.02 $26,652.84
South Korea 51.84 $39,259.10
Spain 50.02 $35,548.77
Saudi Arabia 34.17 $51,940.83
Canada 37.69 $47,063.09
Iran 84.92 $19,311.54
Australia 25.47 $49,005.64
Thailand 68.98 $17,918.91
Egypt 104.12 $11,563.09
Taiwan 23.60 $50,374.85
Poland 38.28 $29,413.05
Nigeria 214.03 $5,237.63

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 19:00
Write a program that displays the following menu: geometry calculator 1. calculate the area of a circle 2. calculate the area of a rectangle 3. calculate the area of a triangle 4. quit enter your choice (1-4): if the user enters 1, the program should ask for the radius of the circle and then display its area. use the following formula: area = ď€(the square of r) use 3.14159 for ď€ and the radius of the circle for r. if the user enters 2, the program should ask for the length and width of the rectangle and then display the rectangle’s area. use the following formula: area = length * width if the user enters 3, the program should ask for the length of the triangle’s base and its height, and then display its area. use the following formula: area = base * height * .5 if the user enters 4, the program should end. input validation: display an error message if the user enters a number outside the range of 1 through 4 when selecting an item from the menu. do not accept negative values for the circle’s radius, the rectangle’s length or width, or the triangle’s base or height. note: if the user enters an improper menu choice (1-4), the program prints "the valid choices are 1 through 4. run the program again and select one of those." if the user enters a negative radius, the program prints "the radius can not be less than zero." if the user enters a negative value for height or base, the program prints "only enter positive values for base and height."
Answers: 1
question
Computers and Technology, 24.06.2019 06:00
Hey i really need some solving this problem: 1. encrypt this binary string into cipher text: 110000. include in your answer the formula the decoder would use to decrypt your cipher text in the format (coded answer) x n mod (m) = y & 2. decrypt this cipher text into a binary string: 106 you.
Answers: 2
question
Computers and Technology, 24.06.2019 12:40
Match the feature to the network architecture. expensive to set up useful for a small organization easy to track files has a central server inexpensive to set up difficult to track files useful for a large organization does not have a central server client- server network peer-to-peer network
Answers: 3
question
Computers and Technology, 24.06.2019 19:00
Luis is cloud-based( microsoft bot framework). true false
Answers: 1
You know the right answer?
Now add code to the function read_gdp_data() so that it looks up the happiness index for each countr...
Questions
question
Mathematics, 04.12.2019 00:31
Questions on the website: 13722367