5 Replies - 931 Views - Last Post: 29 March 2011 - 01:14 AM Rate Topic: -----

#1 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Grade manager not working out grade averages

Posted 26 March 2011 - 10:06 PM

Hi all,

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[1] in subs:
                pass
            else:
                subs.append(i[1])
        scoreLst = []
        """for j in subs:#go through subjects
            for k in all: #work out avgs and add to list
                if k[1] == j[0]:"""
        #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[1]
            name = k[0]
            #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[1] == Subject:
                        if name not in NameDone:
                            print 'l', l
                            count += 1
                            weight += int(l[2]) 
                            total += float(l[3])
                        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[1] == Subject:
                                    Weight_orig = e[2]
                                    Score = e[3]
                                    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[0]
            Total = 0
            for l in FinalLst:
                if l[1] == Cur:
                    Total += l[1]
            Final.append([Cur, Total])
        for i in Final:    """
            #c.execute("""Insert into SubjectAvg (Subject, CurGrade) values 
           # (?,?)""", (i[0], i[1]))
            #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
Maths: 0
English: 47.5
Maths: 0
I should only have each subject once! If anyone could help, that would be great.
Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Grade manager not working out grade averages

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,541
  • Joined: 23-August 08

Re: Grade manager not working out grade averages

Posted 27 March 2011 - 07:46 AM

Dude, too much indentation! Look into splitting that up into functions.

I'd suggest creating a dict of scores by class name, something like this:

classDict = dict()
classDict['English'] = []
classDict['English'].append(score)



Or you could use SQL to calculate your averages as well.
Was This Post Helpful? 1
  • +
  • -

#3 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Re: Grade manager not working out grade averages

Posted 28 March 2011 - 01:22 AM

Thanks for your reply, I will have a go at using the dictionaries.

--Edit--
I just re-did the grade manager with the method you suggested above and it works great thanks again!

This post has been edited by Eric115: 28 March 2011 - 01:59 AM

Was This Post Helpful? 0
  • +
  • -

#4 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Re: Grade manager not working out grade averages

Posted 28 March 2011 - 04:40 AM

If anyone has any ideas on the math formula for using weightings to work out the average, that would be greatly appreciated. Anyone got any ideas?
Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Grade manager not working out grade averages

Posted 28 March 2011 - 06:45 AM

I assume every class has a weight and a grade. For each class, multiply the grade by its corresponding weight and then add that to a total. Divide that total by the sum of all the classes weights, and that should do it... don't quote me on this one though, I haven't had my coffee yet.

This post has been edited by atraub: 28 March 2011 - 06:45 AM

Was This Post Helpful? 1
  • +
  • -

#6 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Re: Grade manager not working out grade averages

Posted 29 March 2011 - 01:14 AM

Thanks atraub, your formula works great!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1