Project 05
Assignment Overview
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:
- 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.
- 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 doesnt return any values.
- 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 doesnt have a return value.
- main(): The main function doesnt have any parameters and doesnt 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
- To clarify the project specifications, sample output is appended to the end of this document.
- Items 1-9 of the Coding Standard will be enforced for this project.
- 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
- 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.
- You should print the happiness scores with two decimal places. (Use the provided formatting)
- The search is not case sensitive, so if user enters united, the program should match United and united.
- You may not use advanced data structures such as dictionaries or classes for this projectno credit if you do. (Note that at this point you likely dont 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
What do you want to search for? united
Country Region Happiness Score
United States North America 7.10
United Kingdom Western Europe 6.72
United Arab Emirates Middle East and Northern Africa 6.57
Average Happiness Score 6.80
Test 2
Input a file name: data.csv
Input 1 to search in country names, 2 to search in regions: 2
What do you want to search for? west
Country Region Happiness Score
Denmark Western Europe 7.53
Switzerland Western Europe 7.51
Iceland Western Europe 7.50
Norway Western Europe 7.50
Finland Western Europe 7.41
Netherlands Western Europe 7.34 Sweden Western Europe 7.29
Austria Western Europe 7.12
Germany Western Europe 6.99
Belgium Western Europe 6.93
Ireland Western Europe 6.91
Luxembourg Western Europe 6.87
United Kingdom Western Europe 6.72
Malta Western Europe 6.49
France Western Europe 6.48
Spain Western Europe 6.36
Italy Western Europe 5.98
North Cyprus Western Europe 5.77
Cyprus Western Europe 5.55
Portugal Western Europe 5.12
Greece Western Europe 5.03
Average Happiness Score 6.69
Test 3
Input a file name: test.txt
Unable to open the file. Please try again.
Input a file name: test.csv
Unable to open the file. Please try again.
Input a file name: data.csv
Input 1 to search in country names, 2 to search in regions: 1
What do you want to search for? states
Country Region Happiness Score
United States North America 7.10
Average Happiness Score 7.10
Test 4
Input a file name: data.csv
Input 1 to search in country names, 2 to search in regions: 3 Invalid choice, please try again!
Input 1 to search in country names, 2 to search in regions: 0 Invalid choice, please try again!
Input 1 to search in country names, 2 to search in regions: file.txt Invalid choice, please try again!
Input 1 to search in country names, 2 to search in regions: data.csv Invalid choice, please try again!
Input 1 to search in country names, 2 to search in regions: 1
What do you want to search for? united
Country Region Happiness Score
United States North America 7.10
United Kingdom Western Europe 6.72
United Arab Emirates Middle East and Northern Africa 6.57
Average Happiness Score 6.80
Test 5
This test is a blind test. You do not get to see input or output.
Reviews
There are no reviews yet.