Question:
- In main, create an empty list.
- Open the file named scores.txt and use a while loop that can detect the end of file to read the scores, and add them to the list and then close the file.
- Call the showscores function with the list of scores as sole argument.
- Inside the showscores function, process the list of scores.
- Print out the average score accurate to two decimal places.
This is scores.txt which contains a list of 5 numbers. The numbers in the file are list vertically like so:
86
92
77
83
96
I have the below code now, but keep getting thiese errors:
line 19, in main()
line 17, in main showscores(scores)
line 2, in showscores sum_scores = sum(scores)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
def showscores(scores):
sum_scores = sum(scores)
average = float(sum_scores // len(scores))
print ("The scores are: " + str(scores))
print ("Average score: " + str(average))
def main():
scores = []
scores_file = open("scores.txt", 'r')
line_list = list(scores_file.readlines())
i = 0
while i < len(line_list):
scores.append(line_list[i])
i += 1
scores_file.close()
showscores(scores)
main()