Home / Expert Answers / Computer Science / hints-use-exception-handling-to-deal-with-the-cases-when-the-file-cannot-be-opened-and-when-there-a-pa273

(Solved): Hints: Use exception handling to deal with the cases when the file cannot be opened and when there a ...



Hints: Use exception handling to deal with the cases when the file cannot be opened and when there are non-numeric percentages on a line in the file. Use an if statement to skip lines with negative numeric values as the final item in the line. Use readlines() to read the information from the file so that student information is grouped into a single string.

Below is the display of the runs of the function on the files found in the assignment template. Note that the file none.txt does not exist, and the file empty.txt is empty.

Write a function progressReport() that takes a string representing the name of a file as a parameter and prints a report about the students represented in the file. The information in the file consists of zero or more lines with the following format: FirstName MiddleName LastName percent, where FirstName is a first name, MiddleName is a middle name, LastName is a last name, and percent is the student's current course percentage. You are guaranteed that each line has four elements to it and that the first three elements will be strings. The last element may or may not be a numeric value, and the function should skip lines that do not have a non-negative, numeric value as the last item in the line. The skipping of non-numeric values must be done using exception handling. The skipping of numeric, negative values should be done with an if statement. Note that you cannot make assumptions about how much or what type of whitespace will separate the four parts on each line. Assuming that the percentage on a line is numeric and non-negative (>= 0), the function prints the following to the screen for each line of the file, one sentence per line: FirstName MiddleInitial. LastName is currently earning a/an grade. FirstName and LastName are formatted as you would expect names to be (e.g., first letter capitalized and the remaining letters in lowercase). The middle initial is the first letter of the middle name in uppercase followed by a period. For a valid percentage (e.g., numeric and >= 0), the grade will be one of 'A', 'B', 'C', 'D', or 'F'. A helper function getGrade() has been provided in the assignment template. The function takes a numeric parameter and returns the grade for a student with that percentage. For full credit you must use getGrade() in your solution. Solutions that duplicate the code found in that function will not earn full credit. Do not forget to close the file once you are done with it. If the file cannot be opened, the function should display a message to that effect and not do any other work. If the file is empty, nothing (not even a blank line) should print to the screen.

>>> progressReport('studentsOne.txt')
Annika A. Settle is currently earning a/an B.
Joon M. Elam is currently earning a/an C.
Jess A. West is currently earning a/an F.
Christopher R. Ling is currently earning a/an D.
Nik C. Franklin is currently earning a/an A.
Elizabeth A. Boyd is currently earning a/an C.
>>> progressReport('studentsTwo.txt')
Leo J. Porter is currently earning a/an B.
John E. Mariast is currently earning a/an D.
>>> progressReport('empty.txt')
>>> progressReport('none.txt')
none.txt could not be opened.



We have an Answer from Expert

View Expert Answer

Expert Answer



Solution:

Here's the implementation of the progressReport() function based on the given requirements:
We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe