Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Must be done in python Assignment Overview An important question about the pandemic is the death rate which is often calculated as deaths per million

Must be done in python

image text in transcribedimage text in transcribedimage text in transcribed

Assignment Overview An important question about the pandemic is the death rate which is often calculated as deaths per million of population. That rate is one metric for how well a country has responded to the pandemic. Once you have the ability to read files in Python you can examine raw data to answer such questions. Program Specifications In this project, you will read a file of data with total Covid deaths and the population of countries. You will then calculate the total deaths per million for countries. Assigning deaths to Covid is not consistently handled among countries and often not consistently within countries. However, there is more consistency with first-world countries so we will extract data for the G20 countries. (There isn't anything special about the G20 except that it makes the assignment more interesting.) Since we are dealing with real data there will be complications. For example, there are columns of data that we aren't interested in such as the deaths in the last seven days and there are rows of data that we aren't interested in such as non-G20 countries. There is one header row that describe columns but they are not data so we need to ignore them. Also, some data is in the thousands with commas and Python cannot directly convert a number with commas into a Python number such as an int or float. You will be asked to write a function to handle commas. Deaths-per-Million high-level algorithm Your high-level algorithm will be: 1. Open an input file for reading 2. Open a different file for writing 3. Write header information to the output file. 4. Loop through the input file, reading it line by line a. Call a function to process each line i. Call a function to handle commas b. Calculate deaths/million-population c. If the data is for a G20 country, display it and write it to a file 5. Close both files 6. Display which countries have deaths-per-million worse than the US File Specification The input file named data.txt has one header line and five fixed-field columns. Column 0 is a country name (in a 25-character field), columns 1, 2, and 3 are integers, and column 4 is a float. The numbers are in 10-character fields. A file such as data.txt with fixed-field columns allows you to use string slicing to extract individual values. 1. You must have and use at least these four functionsmore are fine. A proj05.py file with function stubs is provided. a. def open_file(ch) file_pointer i. If ch is 'w', open a file for writing; otherwise open a file for reading. Repeatedly prompt for a file name until a file is successfully opened. Use the try-except command. Usually, we would use except FileNotFoundError, but that works only for opening a file for reading so we want to use the more general file error for reading and writing so use except IOError. If ch is 'w', the prompt should be 'Enter a file name for writing: else the prompt should be Enter a file name for reading: ' ii. Parameters: ch iii. Returns: file_pointer iv. Display: prompt and error message as appropriate b. def handle commas (s,T) int or float or None i. The parameters are s, a string, and T, a string. The expected values of T is the word"int" or "float"; any other value returns None. If the value of T is "int", the string s will be converted to an int and that int value will be returned. Similar for "float". In both cases, you have to remove the commas from s before converting to int or float. If a value of s cannot be converted to an int or float, None will be returned (hint: use try-except) ii. Parameters: str, str iii. Returns: int or float or None iv. Display: nothing c. def process_line (line) str, int , float i. The parameter is a string which is a line from the file. Extract the country (string in a 25-character field), deaths (int in a 10-character field), and population (float in a 10-character field from the END of the line). Strip leading and trailing spaces from the country name. Return the country, deaths, and population. Use the function handle_commas to process the numbers for deaths and population. ii. Parameters: str iii. Returns: str, int , float iv. Display: nothing i. Call open_ d. def main() Your main algorithm will be as follows calling the above functions. _file to open an input file for reading ii. Call open_file to open a different file for writing iii. Write header information (2 lines) to the output file and to the console. - The header information in the first line is: 'Country', 'Deaths', 'Population', 'Death Rate' - The header information in the second line is: ",", 'Millions', 'per Million' For both lines, use the following formatting for the headers also provided in the strings.txt file): "{:10s}{:>14s}{:>14 s}" iv. Loop through the input file, reading it line by line 1. Call a function to process each line a. Call a function to handle commas 2. Calculate deaths/million-population 3. If the data is for a G20 country, display it and write it to the output file. use the following string formatting for the numbers (also provided in the strings.txt file): "{:10,d}{:>14,.2f}{:>14,.2f}" V. Close both files vi. Display which countries have deaths-per-million worse than the US To determine which countries are worse than the US we provide the deaths-per- million for the US (1277.10). Note that the US rate is rounded to 2 digits after the decimal point. Start with an empty string before the loop and build a comma- separated string of the countries that you will display after the loop. Note that this item is not written to the output file. It is only written to the console. Delive The deliverable for this assignment is the following file: proj05.py -- your source code solution Be sure to use the specified file name and to submit it for grading via Mimir before the project deadline. Notes and Hints: 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 projectnote the change to include more items. 3. You can test functions separatelythat can be a huge advantage in developing correct code faster! If you cannot figure out how to do that, ask your TA for guidance. 4. We provide a string with the names of G20 countries. Hint use the string operator in to see if a country is in the G20. G20 = "Argentina, Australia, Brazil, Canada, China, France, Germany, India, Indonesia, Italy, Japan, South Korea, Mexico, Russia, Saudi Arabia, South Africa, Turkey, United Kingdom, USA, European Union" 5. We also provide a constant for the US death rate per Millions. Hint use this constant to look for the countries that have death rates high than the US. Note that the constant is rounded to 2 digits after the decimal point. US_RATE = 1277.10 6. Do not hard code your solutionsthe result is a zero. Hard coding means that your program for the specific tests rather than having a generic solution. Assignment Overview An important question about the pandemic is the death rate which is often calculated as deaths per million of population. That rate is one metric for how well a country has responded to the pandemic. Once you have the ability to read files in Python you can examine raw data to answer such questions. Program Specifications In this project, you will read a file of data with total Covid deaths and the population of countries. You will then calculate the total deaths per million for countries. Assigning deaths to Covid is not consistently handled among countries and often not consistently within countries. However, there is more consistency with first-world countries so we will extract data for the G20 countries. (There isn't anything special about the G20 except that it makes the assignment more interesting.) Since we are dealing with real data there will be complications. For example, there are columns of data that we aren't interested in such as the deaths in the last seven days and there are rows of data that we aren't interested in such as non-G20 countries. There is one header row that describe columns but they are not data so we need to ignore them. Also, some data is in the thousands with commas and Python cannot directly convert a number with commas into a Python number such as an int or float. You will be asked to write a function to handle commas. Deaths-per-Million high-level algorithm Your high-level algorithm will be: 1. Open an input file for reading 2. Open a different file for writing 3. Write header information to the output file. 4. Loop through the input file, reading it line by line a. Call a function to process each line i. Call a function to handle commas b. Calculate deaths/million-population c. If the data is for a G20 country, display it and write it to a file 5. Close both files 6. Display which countries have deaths-per-million worse than the US File Specification The input file named data.txt has one header line and five fixed-field columns. Column 0 is a country name (in a 25-character field), columns 1, 2, and 3 are integers, and column 4 is a float. The numbers are in 10-character fields. A file such as data.txt with fixed-field columns allows you to use string slicing to extract individual values. 1. You must have and use at least these four functionsmore are fine. A proj05.py file with function stubs is provided. a. def open_file(ch) file_pointer i. If ch is 'w', open a file for writing; otherwise open a file for reading. Repeatedly prompt for a file name until a file is successfully opened. Use the try-except command. Usually, we would use except FileNotFoundError, but that works only for opening a file for reading so we want to use the more general file error for reading and writing so use except IOError. If ch is 'w', the prompt should be 'Enter a file name for writing: else the prompt should be Enter a file name for reading: ' ii. Parameters: ch iii. Returns: file_pointer iv. Display: prompt and error message as appropriate b. def handle commas (s,T) int or float or None i. The parameters are s, a string, and T, a string. The expected values of T is the word"int" or "float"; any other value returns None. If the value of T is "int", the string s will be converted to an int and that int value will be returned. Similar for "float". In both cases, you have to remove the commas from s before converting to int or float. If a value of s cannot be converted to an int or float, None will be returned (hint: use try-except) ii. Parameters: str, str iii. Returns: int or float or None iv. Display: nothing c. def process_line (line) str, int , float i. The parameter is a string which is a line from the file. Extract the country (string in a 25-character field), deaths (int in a 10-character field), and population (float in a 10-character field from the END of the line). Strip leading and trailing spaces from the country name. Return the country, deaths, and population. Use the function handle_commas to process the numbers for deaths and population. ii. Parameters: str iii. Returns: str, int , float iv. Display: nothing i. Call open_ d. def main() Your main algorithm will be as follows calling the above functions. _file to open an input file for reading ii. Call open_file to open a different file for writing iii. Write header information (2 lines) to the output file and to the console. - The header information in the first line is: 'Country', 'Deaths', 'Population', 'Death Rate' - The header information in the second line is: ",", 'Millions', 'per Million' For both lines, use the following formatting for the headers also provided in the strings.txt file): "{:10s}{:>14s}{:>14 s}" iv. Loop through the input file, reading it line by line 1. Call a function to process each line a. Call a function to handle commas 2. Calculate deaths/million-population 3. If the data is for a G20 country, display it and write it to the output file. use the following string formatting for the numbers (also provided in the strings.txt file): "{:10,d}{:>14,.2f}{:>14,.2f}" V. Close both files vi. Display which countries have deaths-per-million worse than the US To determine which countries are worse than the US we provide the deaths-per- million for the US (1277.10). Note that the US rate is rounded to 2 digits after the decimal point. Start with an empty string before the loop and build a comma- separated string of the countries that you will display after the loop. Note that this item is not written to the output file. It is only written to the console. Delive The deliverable for this assignment is the following file: proj05.py -- your source code solution Be sure to use the specified file name and to submit it for grading via Mimir before the project deadline. Notes and Hints: 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 projectnote the change to include more items. 3. You can test functions separatelythat can be a huge advantage in developing correct code faster! If you cannot figure out how to do that, ask your TA for guidance. 4. We provide a string with the names of G20 countries. Hint use the string operator in to see if a country is in the G20. G20 = "Argentina, Australia, Brazil, Canada, China, France, Germany, India, Indonesia, Italy, Japan, South Korea, Mexico, Russia, Saudi Arabia, South Africa, Turkey, United Kingdom, USA, European Union" 5. We also provide a constant for the US death rate per Millions. Hint use this constant to look for the countries that have death rates high than the US. Note that the constant is rounded to 2 digits after the decimal point. US_RATE = 1277.10 6. Do not hard code your solutionsthe result is a zero. Hard coding means that your program for the specific tests rather than having a generic solution

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

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions

Question

What is a verb?

Answered: 1 week ago