Question
Read the happiness file Obtain the program happy_stubs.py along with the input files happiness.csv and world_pop-gdp.tsv from the Homework6 directory on Canvas. Start out by
Read the happiness file Obtain the program happy_stubs.py along with the input files happiness.csv and world_pop-gdp.tsv from the Homework6 directory on Canvas. Start out by filling in the function make_happy_dict() so that it reads the input file happiness.csv which contains the country name, year of the estimate, and the happiness index. The function will return a dictionary mapping country names to their happiness index. Write a loop that reads the open file line by line and adds key value pairs to a dictionary. Extracting the country name and the happiness index as variables and convert the happiness index from a string to a floating point number. Add each converted happiness index to a dictionary indexed by the country name. Modify the return statement so it returns the dictionary you created. To test that your function works, uncomment the call to print_sorted_dictionary() in main(). That function will print a sorted list of key value pairs you added to the dictionary. The last five lines of this list should look like this: Venezuela 5.005663395 Vietnam 5.295547009 Yemen 3.057513952 Zambia 4.041488171 Zimbabwe 3.616479874 Afghanistar Albania Algeria Angola Argentina Armenia Australia Austria Azerbaijan Bahrain Bangladesh Belarus Belgium Belize Benin Bhutan Bolivia Bosnia and Botswana Brazil Bulgaria Burkina Fa: Burundi Cambodia Cameroon Canada Central Afri Chad Chile China Colombia Comoros Congo Costa Rica Cote d'Ivoir Croatia Cuba Cyprus Czech Rep 2018 2.694303 2018 5.004403 2018 5.043086 2014 3.794838 2018 5.792797 2018 5.062449 2018 7.176993 2018 7.396002 2018 5.167995 2017 6.227321 2018 4.499217 2018 5.23377 2018 6.892172 2014 5.955647 2018 5.819827 2015 5.082129 2018 5.915734 2018 5.887401 2018 3.461366 2018 6.190922 2018 5.098814 2018 4.927236 2018 3.775283 2018 5.121838 2018 5.250738 2018 7.175497 2017 3.475862 2018 4.486325 2018 6.436221 2018 5.131434 2018 5.983512 2018 3.97282 2018 5.490214 2018 7.141075 2018 5.268375 2018 5.536271 2006 5.417869 2018 6.276443 2018 7.034165 Portugal Qatar Romania Russia Rwanda Saudi Arab Senegal Serbia Sierra Leor Singapore Slovakia Slovenia Somalia South Afric South Kore South Suda Spain Sri Lanka Sudan Suriname Swaziland Sweden Switzerlanc Syria Taiwan Tajikistan Tanzania Thailand Togo Trinidad an Tunisia Turkey Turkmenist Uganda Ukraine United Arak United King United Stat Uruguay Uzbekistan 2018 5.919823 2015 6.374529 2018 6.150879 2018 5.5135 2018 3.561047 2018 6.356393 2018 4.769377 2018 5.936493 2018 4.305683 2018 6.374564 2018 6.235111 2018 6.249419 2016 4.667941 2018 4.883922 2018 5.840231 2017 2.816622 2018 6.513371 2018 4.400223 2014 4.138673 2012 6.269287 2018 4.211565 2018 7.374792 2018 7.508587 2015 3.461913 2018 6.467005 2017 5.829234 2018 3.445023 2018 6.011562 2018 4.022895 2017 6.19186 2018 4.741132 2018 5.185689 2018 4.620602 2018 4.321715 2018 4.661909 2018 6.603744 2018 7.233445 2018 6.882685 2018 6.371715 2018 6.20546 Uzbekistan Venezuela Vietnam Yemen Zambia Zimbabwe 2018 6.20546 2018 5.005663 2018 5.295547 2018 3.057514 2018 4.041488 2018 3.61648
USE PYTHON
# happy_stubs.py
def main():
# Build dictionary mapping countries to happiness index
happy_dict = make_happy_dict()
# Uncomment to test your make_happy_dict() function
# print_sorted_dictionary(happy_dict)
# Read file containing population and GDP data and add happiness data
read_gdp_data(happy_dict)
def make_happy_dict():
return
def read_gdp_data(happy_dict):
return
# Function prints all the kay value pairs in a dictionary D sorted by key
def print_sorted_dictionary(D):
for key in sorted(D.keys()):
print(key, D[key])
main()
happiness.cvs
USE PYTHON
# happy_stubs.py
def main():
# Build dictionary mapping countries to happiness index
happy_dict = make_happy_dict()
# Uncomment to test your make_happy_dict() function
# print_sorted_dictionary(happy_dict)
# Read file containing population and GDP data and add happiness data
read_gdp_data(happy_dict)
def make_happy_dict():
return
def read_gdp_data(happy_dict):
return
# Function prints all the kay value pairs in a dictionary D sorted by key
def print_sorted_dictionary(D):
for key in sorted(D.keys()):
print(key, D[key])
main()
happiness.cvs
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started