asked 22.5k views
5 votes
write a program that reads student information from a tab separated values (tsv) data file. each row of the data file contains the last name, first name, midterm1 score, midterm2 score, and the final score for each student. a sample of the student information is provided in studentinfo.tsv. assume the number of students is at least 1 and at most 20. assume also the last names and first names do not contain any whitespace. the program then generates a text file named report.txt that records the course grades of the students. both when reading the data fiels and writing the report file, the with statement should be used. the program performs the following tasks:

asked
User Mapet
by
8.0k points

1 Answer

6 votes

Answer:

here's a Python program that reads student information from a tab-separated values (TSV) data file and generates a report of the course grades for each student. The program assumes that the data file is named "studentinfo.tsv" and contains the last name, first name, midterm1 score, midterm2 score, and final score for each student. The report file is named "report.txt".

```python

def calculate_grade(midterm1, midterm2, final):

# calculates the course grade for a student based on their midterm and final exam scores

weighted_avg = 0.25 * midterm1 + 0.25 * midterm2 + 0.5 * final

if weighted_avg >= 90:

grade = "A"

elif weighted_avg >= 80:

grade = "B"

elif weighted_avg >= 70:

grade = "C"

elif weighted_avg >= 60:

grade = "D"

else:

grade = "F"

return grade

with open("studentinfo.tsv", "r") as infile, open("report.txt", "w") as outfile:

# read the student information from the data file and generate a report of course grades

for line in infile:

data = line.strip().split("\t")

last_name, first_name, midterm1, midterm2, final = data

grade = calculate_grade(int(midterm1), int(midterm2), int(final))

outfile.write(f"{last_name}, {first_name}: {grade}\\")

```

In this program, the `calculate_grade` function takes the midterm1, midterm2, and final exam scores for a student as arguments and returns the course grade for that student based on the weighted average of their scores. The course grade is calculated according to the following scale:

- A: 90 or higher

- B: 80 or higher, but less than 90

- C: 70 or higher, but less than 80

- D: 60 or higher, but less than 70

- F: less than 60

The program then uses a with statement to open the data file "studentinfo.tsv" for reading and the report file "report.txt" for writing. It then uses a for loop to read each line of the data file, split the line into its individual fields using the `split()` method with the tab character as the delimiter, and extract the last name, first name, midterm1 score, midterm2 score, and final score for each student.

For each student, the program calls the `calculate_grade` function to determine their course grade based on their midterm and final exam scores. The program then writes the student's last name, first name, and course grade to the report file "report.txt" in the format "last name, first name: grade\\", where "\\" is a newline character.

Finally, the program closes both files using the with statement.

Step-by-step explanation:

please follow me for more if you need any help

answered
User Sebastian Wramba
by
8.8k points