1

I would like to take number of subjects, then take grade letter of each by calling findGradePoint and then return GPA

def findGradePoint(g):
gradeLetter = ["A", "B", "C", "D", "F"]
gradePoint = [4.0, 3.0, 2.0, 1.0, 0.0]

for i, v in enumerate(gradeLetter):
    if v==g:
        gp = gradePoint[i]

return gp




def findGPA(n, ls):
totGp = 0.0
g = len(str,[ls])
for i in g:
    gp = findGradePoint(i)
    totGp = totGp + gp
return round(totGp / len[g],2)

I don't know how to split each value in the array to check grade point for each value and sum all the value then devide them by number count of value in array. Guide me please.

test file

import unittest
import a07

a = a07

class Test(unittest.TestCase):
    def testA(self):
        self.assertEqual(a.findGPA(4, ['B','B','B','C']), 2.75)
    def testB(self):
        self.assertEqual(a.findGPA(4, ['B','B','B','A']), 3.25)
    def testC(self):
        self.assertEqual(a.findGPA(7, ['C','C','C','C','B','B','F']), 2)
    def testD(self):
        self.assertEqual(a.findGPA(4, ['C','D','F','B']), 1.5)


def main():
    unittest.main()

if __name__ == '__main__':
    main()
Cat Fat
  • 13
  • 3

1 Answers1

0

To focus strictly on your code since you seem to already have a working findGradePoint method, you have quite a few problems. I ran your code with the fixed findGPA method, and got it working.

To explain what you are doing wrong:

You defined n in your method definition, but it is not used. I don't think you need it. So you can probably remove it.

def findGPA(n, ls):

The following is incorrect, len only takes one argument. Here is the documentation on len. So this should be removed.

g = len(str,[ls])

Your loop below, since you don't need that g = len(str,[ls]) should be changed form this:

for i in g:

To simply this:

for i in ls

Your sum step, can be easily shortened to just this:

totGp += gp

You again are not using the len() method correctly here:

return round(totGp / len[g],2)

len is a method, and you are treating it like a list. It is to be used as:

return round(totGp / len(ls), 2)

When you make all those corrections, your working method is here:

def findGPA(ls):
    totGp = 0.0
    for i in ls:
        gp = findGradePoint(i)
        totGp += gp
    return round(totGp / len(ls), 2)

Demo:

r = findGPA(['A', 'C', 'D'])
print(r)
# output 2.33
idjaw
  • 25,487
  • 7
  • 64
  • 83
  • Sorry for hard understanding. I alredy got an output for findGradePoint for functon I used above. I'm currently have a bit of problem with findGPA – Cat Fat Oct 22 '15 at 18:27
  • @CatFat look at my updated answer. Let me know what you think. – idjaw Oct 22 '15 at 18:41