In: Computer Science
Python program:
Write a program that reads a text file named test_scores.txt to
read the name of the student and his/her scores for 3 tests. The
program should display class average for first test (average of
scores of test 1) and average (average of 3 tests) for each
student.
Expected Output:
['John', '25', '26', '27']
['Michael', '24', '28', '29']
['Adelle', '23', '24', '20']
[['John', '25', '26', '27'], ['Michael', '24', '28', '29'],
['Adelle', '23', '24', '20']]
Class average for test 1 is: 24.0
Average for student John is 26.00
Average for student Michael is 27.00
Average for student Adelle is 22.33
# function to read file to
# get marks of student
def test_score(filename):
content = []
avg=0
name=[]
average=[]
count=0
test1=0
# open file
f=open(filename,"r")
# read each lines
for line in f:
count=count+1
# content of line
content=line
# convert string to integers and stores in array
numbers = [int(word) for word in content.split() if word.isdigit()]
# get the test1 data for each student and add to test1
test1=test1+numbers[0]
# find current student average score
avg=sum(numbers)/len(numbers)
# get the name of each student
# and store inside list
data= content.split()
name.append(data[0])
# store the average of three subjects
average.append(avg)
# display the result
print("Class average for test 1 is: ",test1/count)
for i in range(count):
print("Average for student ",name[i], " is ","%0.2f" % round(average[i],2))
# main function
def main():
# call function tet_score to
# get the targeted result
test_score("test_scores.txt")
# driver code
if __name__ == "__main__":
main()
___________________________________________________________________
John 25 26 27
Michael 24 28 29
Adelle 23 24 20
___________________________________________________________________
Class average for test 1 is: 24.0
Average for student John is 26.00
Average for student Michael is 27.00
Average for student Adelle is 22.33
___________________________________________________________________
Note: If you have
queries or confusion regarding this question, please leave a
comment. I would be happy to help you. If you find it to be useful,
please upvote.