By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,760 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
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
  1. a=raw_input('Enter the number of courses you are taking: ')
  2. print("")
  3. print("")
  4. b=int(a)
  5. c=range(b)
  6. if int(a) <= 0:
  7.         print ('Error: You must have at least one class!')
  8. d=('Enter the course *NAME*, *CREDITS*, and *GRADE*: ')
  9.  
  10. for i in range(b):
  11.     locals()["y"+str(i)]=raw_input(d).split(',')
  12.     locals()["p"+str(i)]=locals()["y"+str(i)][2]
  13.     locals()["c"+str(i)]=locals()["y"+str(i)][1]
  14. print("")
  15. for i in c:
  16.     if locals()["p"+str(i)]=='a':
  17.         locals()["qp"+str(i)]=4
  18.     elif locals()["p"+str(i)]=='b':
  19.         locals()["qp"+str(i)]=3
  20.     elif locals()["p"+str(i)]=='c':
  21.         locals()["qp"+str(i)]=2
  22.     elif locals()["p"+str(i)]=='d':
  23.         locals()["qp"+str(i)]=1
  24.     elif locals()["p"+str(i)]=='u':
  25.         locals()["qp"+str(i)]=0
  26.     else:
  27.         print("You entered an incorrect grade!")
  28. print("")
  29. print("")
  30. for i in c:
  31.     gradesum=int(locals()["qp"+str(i)])
  32.     k2=sum(gradesum)
  33.     print k2
  34.  
  35. gpa= "dont know yet"
  36. print("Your GPA is calculated to be: "+gpa)
  37.  
Apr 9 '13 #1

✓ answered by bvdet

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)

Share this Question
Share on Google+
3 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
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)
Apr 10 '13 #2

P: 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
Apr 11 '13 #3

bvdet
Expert Mod 2.5K+
P: 2,851
I tried the code snippet below in my IDE.
Expand|Select|Wrap|Line Numbers
  1. >>> [gradeDict.get(item[-1].upper(), 0) for item in inputList]
  2. [4, 2]
  3. >>> 
Apr 11 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.