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
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