**Computer Project #8 Fall 2021**

This assignment focuses on the design, implementation, and testing of a Python program to process a data file and extract meaningful information from it. It is worth 50 points (5% of the course grade) and must be completed no later than 11:59 PM on Monday, November 15, 2021.

**Assignment Background**

In this project, we will explore economic statistics from countries around the world. From the World Bank website, we have collected the following statistics from the year 2018: electricity access (% of population), fertility rate (births per woman), GDP per capita, and life expectancy. The countries are divided into seven regions. The list of the seven regions is:

```
REGION_LIST = ['East Asia & Pacific', 'Europe & Central Asia', 'Latin America & Caribbean', 'Middle East & North Africa', 'North America', 'South Asia', 'Sub-Saharan Africa']
```

In this assignment, you will practice with dictionaries and lists to store and manipulate the data.

**Assignment Specifications**

1. **Read the `data.csv` file** to create a dictionary where the key is a region and the value is a list of lists, with each inner list containing the name of the country and its four values.
2. **Loop prompting** for a valid region.
3. **Loop prompting** for a specific option to display the data.
4. **Provide the following functions:**

- `def open_file() -> file_pointer`
- Open a file for reading. Repeatedly prompt for a file name until a file is successfully opened.
- Use the try-except command:
- `except FileNotFoundError`
- Prompt: `'Input a file: '`
- Error message: `'Invalid filename, please try again.'`
- **Parameters:** None
- **Returns:** file_pointer
- **Display:** Prompt and error message as appropriate

- `def read_file(fp) -> dictionary`
- **Parameter:** A file pointer. Read the whole file to extract data from columns 1 to 7.
- The file is a comma-separated-value file (csv). Use the csv module. There is one header line that needs to be skipped. We are interested in the following columns:
- Country Name
- Region of the country
- Electricity access (% of population)
- Fertility rate (births per woman)
- GDP per capita
- Life expectancy
- **Column Mapping:**
- Column 1: Country Name
- Column 7: Region
- Column 3: Electricity Access
- Column 4: Fertility Rate
- Column 5: GDP per capita
- Column 6: Life Expectancy

- Convert each numeric value to a float. As you read each line, use the region from column 7 as a dictionary key and the value as a list of lists with the inner list containing the country name (Column 1), electricity access (Column 3), fertility rate (Column 4), GDP per capita (Column 5), and life expectancy (Column 6). Use the try-except command to ensure every value in the inner list exists before appending to the outer list. If any values (string or float) are missing, skip that line of the file and do not add the information to the dictionary.

- Each country list will have the following data in this order:
- `L = [country_name, electricity, fertility, GDP, life]`

- The region dictionary will have the region as the key and a list of country lists as the value. For example, if the file has these 4 lines:
```
IND 95.23586 2.222 2005.863 69.416
CAN 100 1.4988 46313.17 81.94878
MDV 100 1.87 10276.93 78.627
TUN 99.8 2.197 3438.789 76.505
```

The dictionary `D` will be:
```
D = {
'South Asia': [
['India', 95.2358551, 2.222, 2005.863005, 69.416],
['Maldives', 100, 1.87, 10276.93282, 78.627]
],
'North America': [
['Canada', 100, 1.4988, 46313.17, 81.94878]
]
}
```

- Don’t add the region to the dictionary unless it is in the file. (Hint: Make sure the region value is not an empty string.)

Answer :

In this computer project, you will be designing, implementing, and testing a Python program to process a data file and extract meaningful information from it.

The data file contains economic statistics from countries around the world, such as electricity access, fertility rate, GDP per capita, and life expectancy. The countries are divided into seven regions: East Asia & Pacific, Europe & Central Asia, Latin America & Caribbean, Middle East & North Africa, North America, South Asia, and Sub-Saharan Africa.

The main algorithm of the program involves the following steps:

Read the data.csv file to create a dictionary. The dictionary will have regions as keys and a list of lists as values. Each inner list will contain the name of the country and its four values.

Prompt the user to enter a valid region from the provided list.

3. Prompt the user to choose a specific option to display the data. This could be the electricity access, fertility rate, GDP per capita, or life expectancy.

Implement the following functions:

a. `open_file()`: This function opens a file for reading. It will repeatedly prompt the user for a file name until a valid file is successfully opened. It uses a try-except block to catch the FileNotFoundError and display an appropriate error message.

b. `read_file(fp)`: This function takes a file pointer as a parameter and reads the whole file to extract the required data. It skips the header line and reads the country name, region, electricity access, fertility rate, GDP per capita, and life expectancy columns. It converts each numeric value to a float and uses the region as the dictionary key and the country information as the value.

Finally, display the requested data based on the user's input.



In this computer project, you will be working on a Python program that focuses on processing a data file and extracting meaningful information from it. The data file contains economic statistics from countries around the world, such as electricity access, fertility rate, GDP per capita, and life expectancy. The countries are categorized into seven regions, namely East Asia & Pacific, Europe & Central Asia, Latin America & Caribbean, Middle East & North Africa, North America, South Asia, and Sub-Saharan Africa.

To accomplish this task, you will follow a step-by-step algorithm. First, you need to read the data.csv file and create a dictionary to store the information. Each region will serve as the key, and the corresponding value will be a list of lists. Each inner list will contain the name of the country along with its electricity access, fertility rate, GDP per capita, and life expectancy.

Next, you will prompt the user to enter a valid region from the provided list. Once the region is selected, you will then prompt the user to choose a specific option to display the data. The available options are electricity access, fertility rate, GDP per capita, and life expectancy.

To facilitate these operations, you will need to implement two functions. The first function, `open_file()`, will handle the process of opening the data file for reading. It will continuously prompt the user for a file name until a valid file is successfully opened. To handle any potential errors, the function will use a try-except block, specifically catching the FileNotFoundError and displaying an appropriate error message.

The second function, `read_file(fp)`, takes a file pointer as a parameter and reads the entire file to extract the required data. This function will utilize the csv module to handle the comma-separated-value format. It will skip the header line and extract the country name, region, electricity access, fertility rate, GDP per capita, and life expectancy columns. For each line, the function will convert the numeric values to float and use the region as the key in the dictionary. The corresponding value will be a list of lists, where each inner list represents the country and its associated information.

After implementing these functions, you will be able to display the requested data based on the user's input. The program will provide the user with a user-friendly interface to interact with the data and extract meaningful insights.

This computer project requires you to design, implement, and test a Python program to process a data file and extract economic statistics from it. The program utilizes dictionaries and lists to store and manipulate the data. It involves reading the data file, prompting the user for a region and specific option, and providing functions to open and read the file. By completing this project, you will gain practical experience in working with data files, dictionaries, and lists in Python, and develop your programming and problem-solving skills.

To know more about GDP visit:

brainly.com/question/31197617

#SPJ11

Other Questions