So I have been trying to write a grade manager. So in the basics of how it works, the user enters in a new grade they got for an assessment (Name of assessment, subject, weighting, mark). This is then taken and stored in a database. This all works fine the problem is I want my program to go through the database, get all of the assessments, and work out the grade average for each subject to be displayed. When I get all the assessments, they are in the form of a list, full of lists e.g. [['Name', 'Subject', weight, mark]]. So I have been trying to use a for loop to iterate through this list, get the first subject, use another loop to look for the other assessments with the same subject, add them into one total and add all of the weightings up. The formula I am using (or trying to use) is if the weightings for any subject add to 100, then the weight is divided by 100, and the score is multiplied by the weight, other wise all of the score are added and divided by how many there are. I don't think this formula is very good, so if anyone can suggest a better one, it would be much appreciated. Anyway, here is my code so far:
def WorkAvgs(self): print 'wkavgs' conn = sqlite3.connect('Data/Tasks.hmw') c = conn.cursor() c.execute("""select * from score""") all = c.fetchall() c.execute("""Delete from SubjectAvg""")# clear this list, we are #re-making it subs =  #Get all of the subjects and add to subs for i in all: if i in subs: pass else: subs.append(i) scoreLst =  """for j in subs:#go through subjects for k in all: #work out avgs and add to list if k == j:""" #subsDone =  FinalLst =  NameDone =  count = 0 for k in all: print 'k', k #new way of doing averages, because it works better #If all of the weightings add to 1 or 100, then we #work the weighted average (below method, then add them #all together) #If, however the weightings don't add to 1 or 100 then #just add all of the scores and divide by how many there are #Still need to test this way but! Subject = k name = k #if Subject not in subsDone: if name not in NameDone: total = 0 weight = 0 for l in all: #check here to see if the subject is the same as the current #then add it to the list, with a total #also needs to check to see if the weightings all add to 100 # a loop with in a loop with in a loop! if l == Subject: if name not in NameDone: print 'l', l count += 1 weight += int(l) total += float(l) else: print 'passed' pass if weight > 99.5: if name not in NameDone: print 'FinalLst', FinalLst #print 'SubsDone', subsDone print 'scoreLst', scoreLst print 'names', NameDone weight = 100 for e in all: print 'e', e if e == Subject: Weight_orig = e Score = e Weight = float(Weight / 100.0) finish = float(Score * Weight) FinalLst.append([Subject, finish]) # subsDone.append(Subject) NameDone.append(name) else: pass else: if name not in NameDone: finish1 = float(total / count) finish = '%.3f' % finish1 FinalLst.append([Subject, finish]) #subsDone.append(Subject) NameDone.append(name) else: pass """count = -1 # don't need this anymore, might use it later though? Final =  for j in FinalLst: count += 1 Cur = j Total = 0 for l in FinalLst: if l == Cur: Total += l Final.append([Cur, Total]) for i in Final: """ #c.execute("""Insert into SubjectAvg (Subject, CurGrade) values # (?,?)""", (i, i)) #conn.commit() # c.close() #conn.close() print 'FinalLst', FinalLst # print 'SubsDone', subsDone #print 'scoreLst', scoreLst - not using anymore print 'names', NameDone self.components.mclGrade.items = FinalLst
I should mention, that some things in the code aren't actually being used because I have been changing it a lot trying to figure it out.
After I run the code, I get some results where it is just the score of the test, others where it has worked something out completely wrong and some are just equal to 0.
For example, I am running it with this list:
[['Test1', 'English', 15, 95], ['Test2', 'Maths', 10, 45], ['test3', 'English', 10, 75], ['test4', 'Maths', 10, 35]]
After I run the program, I get this output:
English : 95
I should only have each subject once! If anyone could help, that would be great.
Thanks in advance