Need python help taking sum...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jlandry287
    New Member
    • Apr 2013
    • 3

    Need python help taking sum...

    So what I'm trying to do is get the sum of quality points ("qp")

    Code:
    a=raw_input('Enter the number of courses you are taking: ')
    print("")
    print("")
    b=int(a)
    c=range(b)
    if int(a) <= 0:
            print ('Error: You must have at least one class!')
    d=('Enter the course *NAME*, *CREDITS*, and *GRADE*: ')
    
    for i in range(b):
        locals()["y"+str(i)]=raw_input(d).split(',')
        locals()["p"+str(i)]=locals()["y"+str(i)][2]
        locals()["c"+str(i)]=locals()["y"+str(i)][1]
    print("")
    for i in c:
        if locals()["p"+str(i)]=='a':
            locals()["qp"+str(i)]=4
        elif locals()["p"+str(i)]=='b':
            locals()["qp"+str(i)]=3
        elif locals()["p"+str(i)]=='c':
            locals()["qp"+str(i)]=2
        elif locals()["p"+str(i)]=='d':
            locals()["qp"+str(i)]=1
        elif locals()["p"+str(i)]=='u':
            locals()["qp"+str(i)]=0
        else:
            print("You entered an incorrect grade!")
    print("")
    print("")
    for i in c:
        gradesum=int(locals()["qp"+str(i)])
        k2=sum(gradesum)
        print k2
        
    gpa= "dont know yet"
    print("Your GPA is calculated to be: "+gpa)
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Creating a named variable for each value is a waste of resources. Manipulating locals() is generally a bad idea. I would suggest that you compile a list of user input and use a dictionary to translate letter grades to grade points. The following is untested. It uses a list comprehension to sum the grade points, but you could use a for loop just as well.
    Code:
    # b = number of courses taken
    
    gradeDict = {"A": 4, "B": 3, "C": 2, "D": 1, "F": 0}
    inputList = []
    
    for i in range(b):
        while True:
            newdata = raw_input("Enter name, credits, and grade"
                                "('A'...'F') separated by commas")
            if newdata[-1].upper() not in gradeDict.keys():
                print "You entered an invalid grade. Try again."
            else:
                inputList.append(newdata.split(","))
                break
    
    # Using dict method get() and a default value of 0 ensures the calculation
    # does not fail due to a missing comma
    GPA = sum([gradeDict.get(item[-1].upper(), 0) for item in inputList])/float(b)

    Comment

    • jlandry287
      New Member
      • Apr 2013
      • 3

      #3
      ok i got it working, but it is not calculating correctly. How can i pull all of the "grades" and take the sum? that is the main part i'm having trouble with.

      P.S., thanks for your insight, it works much cleaner

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        I tried the code snippet below in my IDE.
        Code:
        >>> [gradeDict.get(item[-1].upper(), 0) for item in inputList]
        [4, 2]
        >>>

        Comment

        Working...