Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This is the right code: #happy 3 . py #Mark Choi # # # # # def main ( ) : #define main function happy
This is the right code:
#happypy
#Mark Choi
#
#
#
#
#
def main: #define main function
happydict makehappydict #Create dictionary mapping all countires to its relative happiness index
printsorteddictionaryhappydict #print the key value pairs that're sorted by key
lookuphappinessbycountryhappydict #query happiness dictionary
def makehappydict: #will read the 'happiness.scv file that'll build a dictionary mapping countries to their relative happiness index
#I made an empty dictionary to store happiness data index by country
happydict
#opens file 'happiness.csv for reading.
with openhappinesscsvr as file:
#ignores the header line of CSV file. skip first line header;contains column names.
file.readline
#reads each line in file using a "for" loop.
for line in file:
#strips any leading or trailing whitespace, then split the line into columns using a comma.
columns line.stripsplit
#extracts country name st element of split line and happiness index rd element of split line
country columns
happinessindex columns
#this adds country name as the key, while the happiness index is the value to the dictionary
happydictcountry happinessindex
#returns dictionary storing countries and their happiness indexes
return happydict
def lookuphappinessbycountryhappydict: #continuously allows users to enter country name to view its happiness index in dictionary.If not found, and error message will appear.
#created an indefinite loop to continuously ask for the users input
while True:
#allows user to enter a country name or 'done' to exit
country inputEnter a country to lookup or 'done' to exit:
#reviews if user wants to exit loop
if country.lower 'done':
#'break' the loop if user entered 'done'
break
#reviews if entered country is in the dictionary
if country in happydict:
#prints happiness index of the country if found in dictionary
printhappydictcountry
else:
#prints a not 'found message' if the country isn't in the dictionary
printfcountry not found"
def printsorteddictionaryhappydict: #prints all keyvalue pairs in the dictionary that're organzied by key
#prints header indicating the contents are sorted by key
printContents of dictionary sorted by key."
#prints column headers for keyvalue pairs
printKey Value"
# Iterate over the keys of the dictionary, sorted in ascending order
for key in sortedhappydict.keys:
#prints every key and its correlated value from the dictionary
printkey happydictkey
main #call main function to execute script
Now it outputs the correct results asked in the directions. However, it prints the whole dictionary first. How do I get rid of that
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