46.2k views
3 votes
(Process scores in a text file) Suppose that a text file contains an unspecified number of scores. Write a program that reads the scores from the file and displays their total and average. Scores are separated by blanks. Your program should prompt the user to enter a filename. Here is a sample run:

1 Answer

5 votes

Answer:

Here is the Python program:

def scores(file): # method scores that takes a file name as parameter and returns the sum and average of scores in a file

with open(file, 'r') as infile: # open the file in read mode

lines = [score.split() for score in infile] # split the scores into a list

print("The scores are:",lines) #print the scores

for line in lines: # loops through each score

total= sum(int(score) for score in line) # adds the scores

average =total/len(line) # computes average by taking sum of scores and dividing by number of scores in file

print("The sum is:", total) #prints the sum of scores

print("The average is:", "{:.2f}".format(average)) #prints the average

filename = input("Enter name of the file: ") #prompts user to enter name of file

scores(filename) #calls scores method by passing the file name to it in order to compute sum and average of file contents i.e. scores

Step-by-step explanation:

It is assumed that the scores in the file are separated by a blank space.

The scores() method takes a file name as parameter. Then it opens that input file in read mode using object infile.

split() method is used to split the scores in a file into a list. Suppose the scores are 1 2 3 4 5 6 . So after the split, they become ['1', '2', '3', '4', '5', '6']

The loop iterates through each score in the file, splits them into a list and stores this list in lines. The next print statement prints these scores in a list.

The second loop for line in lines iterates through each score of the list and the statements: total= sum(int(score) for score in line) and average =total/len(line) computes the total and average of scores.

total= sum(int(score) for score in line) statement works as follows:

for loop iterates through each element of list i.e. each score

int() converts that string element into integer.

sum() method adds the integers to compute their total.

So if we have ['1', '2', '3', '4', '5', '6'] each element i.e. 1,2,3,4,5,6 is converted to integer by int() and then added together by sum method. So this becomes 1+2+3+4+5+6 = 21. This result is stored in total. Hence

total = 21.

average = total/len(line) works as follows:

The computed sum of scores stored in total is divided by the number of scores. The number of scores is computed by using len() method which returns the length of the line list. So len() returns 6. Hence

average = total/len(line)

= 21 / 6

average = 3.5

The next two print statement prints the value of sum and average and "{:.2f}".format(average)) prints the value of average up to 2 decimal places.

The screenshot of the program along with its output is attached.

(Process scores in a text file) Suppose that a text file contains an unspecified number-example-1
User Ari Ronen
by
5.8k points