Home / Expert Answers / Computer Science / python-write-a-function-studentreport-that-takes-a-string-representing-the-name-of-a-file-as-a-p-pa388

(Solved): Python Write a function studentReport() that takes a string representing the name of a file as a p ...



Python

Write a function studentReport() that takes a string representing the name of a file as a parameter and prints a report using the information in the file. The information in the file consists of zero or more lines with the following format: FirstName Name0 Name1 ... NameN LastName credit where FirstName is a first name, Name0 through NameN are an arbitrary number of middle names, LastName is a last name, and credit is a number that represents the number of credits the student has earned at DePaul University. The first name and middle names may or may not be present for each line of the file. You are guaranteed that every line has a minimum a last name and a number of credits. The items are separated by some amount of whitespace. You may not make assumptions about how much or what type of whitespace is present between the items. Assuming that the amount of credit the student has earned is an integer and non-negative (>= 0), the function prints one of two things to the screen for each line of the file, one sentence per line. If the line has a first name as well as a last name (and possibly some number of middle names), it prints the following for that line: FirstName LastName is a level. If the line omits the first name and middle name(s) and thus only has a last name, it prints the following for that line: LastName is a level. In each case FirstName and LastName are formatted as you would expect names to be, e.g., first letter capitalized and the remaining letters in lowercase, and level is the enrollment classification for the student based on the number of credits they have earned. For valid credit hours, the level classification will be one of 'freshman', 'sophomore', 'junior', or 'senior'. A version of the getLevel() you wrote for a previous lab has been provided. The function takes a numeric parameter and returns the classification for students with that many credits. For full credit, you must use the getLevel() method rather than duplicating its code in the studentReport() function. Note that the function should skip any students who have a negative, floating point, or non-numeric amount of credit. The students2.txt and students3.txt file has examples of students like this. Do not forget to close the file once you are done with it. If the file is empty, nothing should print to the screen. If the file cannot be opened, a message indicating that should be printed to the screen but no other work should be done.

Hints: Use exception handling to deal with the cases when the file cannot be opened and when there are non-numeric or decimal credits in the file. Use an if statement to distinguish between lines that have only a last name and lines that have a first and last name and possibly some middles names. Use an if statement to distinguish between positive and negative integer values for the credit hours. 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 files none1.txt and none2.txt do not exist, and the file empty.txt is empty.

>>> studentReport('none1.txt')
none1.txt could not be opened.
>>> studentReport('none2.txt')
none2.txt could not be opened.
>>> studentReport('empty.txt')
>>> studentReport('students1.txt')
Settle is a junior.
Elam is a senior.
West is a freshman.
Ling is a sophomore.
Franklin is a freshman.
Boyd is a senior.
>>> studentReport('students2.txt')
Leo Porter is a freshman.
Michalis Giannakos is a senior.
Claudia Szabo is a sophomore.
>>> studentReport('students3.txt')
Jojo Settle is a junior.
Elias West is a freshman.

# A helper function

# Do not modify

def getLevel(credit):

if credit < 0:

return 'invalid'

elif credit < 44:

return 'freshman'

elif credit < 88:

return 'sophomore'

elif credit < 132:

return 'junior'

else:

return 'senior'



We have an Answer from Expert

View Expert Answer

Expert Answer



Let's implement the function studentReport as per the given requirements:
We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe