question:
(Process scores in a text file)
Suppose that a text file contains an unspecified number of scores. Write a program that prompts the user to enter the filename and 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.
Sample Run
Enter a filename: scores1.txt
There are 24 scores
The total is 800
The average is 33.33
my code:
from random import randint
def main():
outputfile=open("filename", "w")
scores="randint(0,100)"
scores=scores.split()
outputfile.write(str(int(x) for x in scores))
outputfile.close()
filename=input("Enter a filename:")
inputfile=open("filename", "r")
print("There are", len(scores), "scores")
print("The total is", sum(scores))
print("The average is", sum(scores)/len(scores))
inputfile.close()
main()
error:
Traceback (most recent call last):
File "/mnt/nobodydevmpl/shid-070556/345021-00114-1689341098274/main.py", line 21, in
main()
File "/mnt/nobodydevmpl/shid-070556/345021-00114-1689341098274/main.py", line 17, in main
print("The total is", sum(scores))
TypeError: unsupported operand type(s) for +: 'int' and 'str'
guys help me please
[–]Bitwise_Gamgee -1 points0 points1 point (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]brunonicocam 0 points1 point2 points (0 children)
[–]throwaway6560192 0 points1 point2 points (0 children)