This assignment involves coding and testing of a program that reads input files, and uses strings and functions to search through the file and print happiness scores of matching countries.
Assignment Background
The World Happiness Report is a survey of the state of global happiness. The report gets released each year at the United Nations at an event celebrating International Day of Happiness. For this project we are using the 2017 world happiness data which you can download from http://worldhappiness.report/ or https://www.kaggle.com/unsdsn/world-happiness/
We modified the data file and provided data.csv to use for this project.
Project Description
Your program must prompt for a file name at program start and open it to read. Then prompt the user to search in country name or region. Finally prompt for a keyword to search. The program should read the file line by line and print the matching results. At the end, the program should print the average happiness scores of the printed results.
The data file has multiple columns and we use only the first three in this project: country, region and happiness score.
You have to use the four functions in the provided proj05.py skeleton. You have to implement and use the following:
1. open_file(): Prompts for a file name and opens it. Returns a file pointer. Keeps looping until a file is successfully opened.
Use try-except and FileNotFoundError.
2. read_data(fp, input_str, search_str): Has three parameters, file pointer fp, the input string input_str which is either “1” or “2”, and the keyword search_str. It reads through the file and calls display_line function for every line that contains the search_str for the specified input_str field (“1” for country; “2” for region). After printing all lines, an average happiness score for those selected countries is printed. This function doesn’t return any values.
3. display_line(country_name, region_name, happiness_score): Has three inputs, country name, region and happiness score. It prints the values using the provided formatting. This function doesn’t have a return value.
4. main(): The main function doesn’t have any parameters and doesn’t return any values. It is responsible for prompting the user for inputs and calling the appropriate functions.
Assignment Deliverable
The deliverable for this assignment is the following file:
proj05.py – the source code for your Python program
Be sure to use the specified file name and to submit it for grading via the Mimir before the project deadline.
Assignment Notes
1. To clarify the project specifications, sample output is appended to the end of this document.
2. Items 1-9 of the Coding Standard will be enforced for this project.
3. Use a while loop in open_file function to loop again if the file fails to open. To check if the file opened correctly use a try-except statement.
4. When the program prompts the user to input either “1” or “2”, it should check the input and print an error message and prompt the user for another input in case of an error. This should be done in a while loop as the user might enter a wrong value multiple times.
5. You should print the happiness scores with two decimal places. (Use the provided formatting)
6. The search is not case sensitive, so if user enters “united”, the program should match “United” and “united”.
7. You may not use advanced data structures such as dictionaries or classes for this project—no credit if you do. (Note that at this point you likely don’t recognize those terms because they are covered later in the course.)
Test Cases
Test 1
Input a file name: data.csv
Input 1 to search in country names, 2 to search in regions: 1