jeudi 15 octobre 2015

Function produces output alphabetically, order by average score instead

This function shows the average score for each person in database from 3 most recent results. The output is ordered by name, how can I get it to order by average, highest to lowest.

def averagescore():
allnames = []
itr = cursor.execute("SELECT name FROM "+group+" GROUP BY name;").fetchall()#lists names in database
for x in itr:

    if x not in allnames:
        allnames.append(x[0])
    else:
        False
allnames.sort()
avg = 0
number = 0

for name in allnames:

    cursor.execute("SELECT total FROM "+group+" WHERE (\""+name+"\") = NAME ORDER BY DATE DESC LIMIT 3;")#3 most recent scores
    scores =(cursor.fetchall())     

    scores = [i[0] for i in scores]        
    avg = sum(scores)/len(scores)
    print(name,"'s average is: ",avg,". From (up to) 3 most recent scores which were: ",scores,)
    avg = 0

The output is:

AZ 's average is:  0.0 . From (up to) 3 most recent scores which were:  [0]
ED 's average is:  0.6666666666666666 . From (up to) 3 most recent scores which were:  [0, 1, 1]
JAKE 's average is:  2.0 . From (up to) 3 most recent scores which were:  [0, 4]
SAM 's average is:  0.0 . From (up to) 3 most recent scores which were:  [0]
ZAC 's average is:  0.0 . From (up to) 3 most recent scores which were:  [0]

I would like it to be:

JAKE 's average is:  2.0 . From (up to) 3 most recent scores which were:  [0, 4]
ED 's average is:  0.6666666666666666 . From (up to) 3 most recent scores which were:  [0, 1, 1]

ETC

Aucun commentaire:

Enregistrer un commentaire