464,760 Members | 1,123 Online Need help? Post your question and get tips & solutions from a community of 464,760 IT Pros & Developers. It's quick & easy.

# Need python help taking sum...

 P: 3 So what I'm trying to do is get the sum of quality points ("qp") Expand|Select|Wrap|Line Numbers 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)]     locals()["c"+str(i)]=locals()["y"+str(i)] 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)   Apr 9 '13 #1

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.
Expand|Select|Wrap|Line Numbers
1. # b = number of courses taken
2.
3. gradeDict = {"A": 4, "B": 3, "C": 2, "D": 1, "F": 0}
4. inputList = []
5.
6. for i in range(b):
7.     while True:
8.         newdata = raw_input("Enter name, credits, and grade"
9.                             "('A'...'F') separated by commas")
10.         if newdata[-1].upper() not in gradeDict.keys():
11.             print "You entered an invalid grade. Try again."
12.         else:
13.             inputList.append(newdata.split(","))
14.             break
15.
16. # Using dict method get() and a default value of 0 ensures the calculation
17. # does not fail due to a missing comma
18. GPA = sum([gradeDict.get(item[-1].upper(), 0) for item in inputList])/float(b) 