12.0k views
1 vote
Write a program to compute numeric grades for a course. The course records are in a file that will serve as the input file. The input file is in exactly the following format: Each line contains a student's last name, then on espace, then the student's first name, then one space, then ten quiz scores all on one line. The quiz scores are whole numbers and are separated by one space. Your program will take its input from this file and send its output to a second file. The data in the output file will be the same as the data in the input file except that there will be one additional number ( of type double ) at the end of each line. This number will be the average of the student's ten quiz scores. If this is being done as a class assignment, obtain the file names from your instructor

1 Answer

1 vote

Answer:

# Open the input and output files

with open('input.txt', 'r') as infile, open('output.txt', 'w') as outfile:

# Loop over each line in the input file

for line in infile:

# Split the line into the student's name and their quiz scores

name, *scores = line.split()

# Convert the quiz scores to integers

scores = [int(score) for score in scores]

# Calculate the average quiz score

avg_score = sum(scores) / len(scores)

# Write the output to the output file

outfile.write(f"{name} {' '.join(map(str, scores))} {avg_score:.2f}\\")

Step-by-step explanation:

Assuming that the input file is named input.txt, the output file will be named output.txt. You can modify the file names and paths as necessary to fit your requirements.

User JorgeM
by
7.6k points