Code writing questions to read scores from Python text files

Asked 2 years ago, Updated 2 years ago, 13 views

I need to save Python's entered score as a text file and write a code to get the total and average by reading the score from the text file, but I've never written a code related to the text file, so can you write a code and show me? And the scores that you bring to a text file are treated as characters, so you need to convert them into numbers.

python

2022-09-20 19:34

1 Answers

test.txt:

32
43
54

test.py:

sum = 0
file = open('test.txt', 'r')
point = file.readlines()
for i in point:
    equal to sum += i #sum=sum+i
print(sum/len(point))

To change the result to a natural number, import the math module and

math.floor(num) #Down
Math.ceil(num) #Raised
Round(num) #Round

Use these functions. (round function built-in)


2022-09-20 19:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.