Answered step by step
Verified Expert Solution
Link Copied!

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:
#happy3.py
#Mark Choi
#
#
#
#
#
def main(): #define main function
happy_dict = make_happy_dict() #Create dictionary mapping all countires to its relative happiness index
print_sorted_dictionary(happy_dict) #print the key value pairs that're sorted by key
lookup_happiness_by_country(happy_dict) #query happiness dictionary
def make_happy_dict(): #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)
happy_dict ={}
#opens file 'happiness.csv' for reading.
with open('happiness.csv','r') 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.strip().split(',')
#extracts country name (1st element of split line) and happiness index (3rd element of split line)
country = columns[0]
happiness_index = columns[2]
#this adds country name as the key, while the happiness index is the value to the dictionary
happy_dict[country]= happiness_index
#returns dictionary storing countries and their happiness indexes
return happy_dict
def lookup_happiness_by_country(happy_dict): #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 = input("Enter 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 happy_dict:
#prints happiness index of the country if found in dictionary
print(happy_dict[country])
else:
#prints a not 'found message' if the country isn't in the dictionary
print(f"{country} not found")
def print_sorted_dictionary(happy_dict): #prints all key-value pairs in the dictionary that're organzied by key
#prints header indicating the contents are sorted by key
print("Contents of dictionary sorted by key.")
#prints column headers for key-value pairs
print("Key Value")
# Iterate over the keys of the dictionary, sorted in ascending order
for key in sorted(happy_dict.keys()):
#prints every key and its correlated value from the dictionary
print(key, happy_dict[key])
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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions