Starting from:

$25

CSE231- Project 05 Solved

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:

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

 

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.

 

Grading Rubric
 

5 pts      Coding Standard 

(descriptive comments, mnemonic identifiers, format, etc...)

 

8 pts Test 1, search in country

8 pts Test 2, search in region

8 pts Test 3, error checking for file

8 pts Test 4, error checking for input (1 or 2)

8 pts Test 5 (Blind test)

More products