简体   繁体   中英

Python File reading and writing

I'm currently coding a program that is supposed to:

1) Open a file which has names along with grades on each line (I have successfully done this and put the contents of each line into an array).

2) Evaluate the data and find the max, min, and average.

I am stuck in the second part because if I try to use the max or min function pertaining to the array, I have an error code that pops up, since the values in the array are technically a string.

So my question is how do I only get the value of the numbers? Would a parallel list be more useful and if so how would I implement that?

Here's my code so far:

def getGrades(filename):

    try:
        gradeArray = []
        with open("Grades.txt", "r") as f:
            for line in f:
                gradeArray.append(line.find())
    except:
        print("Could not open", filename)
    return(gradeArray)

Optimize as needed:

minValue = 0
maxValue = 0
total = 0
first = int(gradeArray[0])
total = first
for i in range(1, len(gradeArray)):
    minValue = min(first, int(gradeArray[i]))
    maxValue = max(first, int(gradeArray[i]))
    total = total + int(gradeArray[i])
average = total / len(gradeArray)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM