I would like to sort content from a a text file. I have already sorted them by alphabetical order (name). The problem i am getting is that when I try to sort the scores in ascending or decending order, the program does not recognise the 10 as a 10 but as a 1. I have attached my code and text file.
Python 3.4 Code:
import csv
import operator
quizGrades= open('Quiz-1.txt' , 'r')
def menu():
print('To Check The scores by ALPHABETICLE Order [1]')
print('To Check The Scores In ASCENDING Order [2]')
print('To check the AVERAGE Scores of Each Student [3]')
choice=int(input(':'))
if choice==1:
alpha()
elif choice==2:
ascend()
def alpha():
quizGrades = open('Quiz-1.txt' , 'r')
grades = csv.reader(quizGrades, delimiter =',')
sortedGrades = sorted(grades, reverse= False, key=operator.itemgetter(1))
for eachline in sortedGrades:
print()
print (eachline)
quizGrades.close()
def ascend():
quizGrades = open('Quiz-1.txt' , 'r')
grades = csv.reader(quizGrades, delimiter =',')
sortedGrades = sorted(grades, reverse= False, key=operator.itemgetter(4))
for eachline in sortedGrades:
print()
print (eachline)
quizGrades.close()
menu()
Text file has (Edit: changed names for annonymity sake)
Name:,Aum Patel,Class:1,Score:,10,/10
Name:,Lolly Pop,Class:1,Score:,9,/10
Name:,Kimjon Un,Class:1,Score:,5,/10
Name:,Preet Are,Class:1,Score:,7,/10
Name:,Aum Patel,Class:1,Score:,8,/10
Name:,Bab Singh,Class:1,Score:,2,/10
Name:,Kimjon Un,Class:1,Score:,1,/10
Name:,Aum Patel,Class:1,Score:,10,/10
Name:,Chong Singh,Class:1,Score:,1,/10
Name:,Bab Singh,Class:1,Score:,7,/10
Name:,Ak Chan,Class:1,Score:,8,/10
ascend
function (the one that isn't working for you) and remove things such as your menu. Be sure to checkout the tour for more information. stackoverflow.com/tour