In this assignment we are going to practice reading from a file and writing the results of your program into a file. We are going to write a program to help Professor X automate the grade calculation for a course. Professor X has a data file that contains one line for each student in the course. The students name is the first thing on each line, followed by some tests scores. The number of scores might be different for each student. Here is an example of what a typical data file may look like: Joe 10 15 20 30 40 Bill 23 16 19 22 Sue 8 22 17 14 32 17 24 21 2 9 11 17 Grace 12 28 21 45 26 10 John 14 32 25 16 89 Program details Write a program that: Ask the user to enter the name of the data file. Read the data from the file and for each student calculate the total of the test scores, how many tests the student took, and the average of the test scores. Save the results in an output file (e.g. stats.txt) in the following order: studentName totalScore numberOfTests testAverage Note that the average is saved with 2 decimal places after the decimal point. Validation: Make sure your program does not crash if a file is not found or cannot be open.

Respuesta :

Answer:

In Python:

import os.path

from os import path

fname = input("Filename: ")

if path.exists(fname):

with open(fname) as file_in:

 lines = []

 for line in file_in:

  lines.append(line.rstrip('\n'))

f = open("stat.txt","r+")

f.truncate(0)

f = open("stat.txt", "a")

f.write("Names\tTotal\tSubjects\tAverage\n")

for i in range(len(lines)):

 rowsum = 0; count = 0

 nm = lines[i].split()

 for j in nm:

  if (j.isdecimal()):

   count+=1

   rowsum+=int(j)

 average = rowsum/count

 f.write(nm[0]+"\t %3d\t %2d\t\t %.2f\n" % (rowsum, count, average))

f.close()

else:

print("File does not exist")

Explanation:

See attachment for explanation where comments were used to explain each line

Ver imagen MrRoyal

For each student, the sum of the marks, total tests were taken and the average of marks is calculated and is stored in an output file, for example, stats.txt.

Python Code:

The code in Python asks the user for an input file name. If the input file is not found an error is printed else the data in the file is processed further.

Try:

  # take input file name

  file = input('Enter name of the data file: ')

# open the input file

  ip = open(file, 'r')

# store the lines from input file in a list

  lines = ip.readlines()

  # close the file

  ip.close()

# open the output file

  op = open('stats.txt', 'w')

# iterate over each line

  for line in lines:

 # split data using space as delimiter

      data = line.split()

 # get name

   name = data[0]

 # get marks and convert them to integer

      marks = list(map(int, data[1:]))

# get sum of marks

      total = sum(marks)

# get number of tests

      length = len(marks)

# calculate average of marks

      avg = total/length

# add the data to output file

      op.write('{} {} {} {:.2f}\n'.format(name, total, length, avg))

# close the output file

  op.close()

print('Stats have been save in the output file')

# catch FileNotFoundError

except FileNotFoundError:

# print error

  print('Error: that file does not exist. Try again.')

Learn more about the topic python code:

https://brainly.com/question/14492046