Solving for average:
To get the average you need to add all the scores together, then divide it by the number of scores. You can do this by iterating through the lines and summing up all the scores, then divide by the number of lines
Sorting by score:
You need to call the sorted() function and provide your own key. You had a function that almost did it, I just fixed it up a little bit. You send it the list of lines and your key that returns the score, then reverse it since you want them highest to lowest. Then it's just a matter of looping through your new sorted list and printing each line
Overall Comments
The structure of this program is very messy and redundant. You should just read the file once, then figure everything out. Iterating through the file inside every if statement is just slow. You should also use functions for a lot of this. Make a function that returns the average, a function that return the list ordered by score etc. Leaving the code all scrambled in the main just makes it hard to read
I've implemented these in the code below, but I suggest you try them on your own now that you understand what to do and only use this code as a reference if you get stuck
sort = input("What would you like to do with this class? Put them into alpabetical order(a)? Average the scores(b)? Highest to lowest(c)?") wf = "file.txt" with open(wf, 'r') as r: if sort == 'a': for line in sorted(r): print(line, end = '') elif sort == 'b': totalScore = 0 numOfScores = 0 for line in sorted(r): numOfScores += 1 totalScore+= int(line.split('score = ')[1]) average = totalScore / numOfScores print(average) elif sort == 'c': def score(line): return int(line.split('=')[1]) with open(wf, 'r') as r: linesList = r.readlines() sortedList = sorted(linesList, key=score) for line in sortedList: print(line.rstrip("\n"))
For this example I used your provided example scores file, like such:
bob - score = 12 harry - score = 1 ellis - score = 21 sam - score = 30