Student GPA

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2006
Posts: 34
Reputation: babutche is an unknown quantity at this point 
Solved Threads: 0
babutche babutche is offline Offline
Light Poster

Student GPA

 
0
  #1
Jan 11th, 2007
Hi,

I am trying to average student GPA. I thought I had this problem solved but then I ran it and the GPA did not come out correct. Can anyone give any suggestions?



  1. import string
  2. import math
  3. class Student:
  4. def __init__(self, name, hours, qpoints):
  5. self.name = name
  6. self.hours = float(hours)
  7. self.qpoints = float(qpoints)
  8. def getName(self):
  9. return self.name
  10. def getHours(self):
  11. return self.hours
  12. def getQpoints(self):
  13. return self.qpoints
  14. def gpa(self):
  15. return self.qpoints/self.hours
  16. def addGrade(self, gradePoint, credits):
  17. self.hours = credits
  18. self.qpoints = credits*gradePoint
  19. def main():
  20. print "This program is a modified version of the student class. It adds"
  21. print "a mutator method that records a grade and calculates the GPA for"
  22. print "the student"
  23. print
  24.  
  25. stu = Student("stu", 0.0, 0.0)
  26. while 1:
  27. try:
  28. print
  29. grade_str = raw_input("Enter gradepoint or (Enter to quit): ")
  30. if grade_str == "":
  31. break
  32. grade = float(grade_str)
  33. try:
  34. grade_int = int(grade_str)
  35. print "Error, use floating point number"
  36. print
  37. grade_str = raw_input("Enter gradepoint or (Enter to quit): ")
  38. except:
  39. print grade
  40. except ValueError:
  41. print "Error, use floating point number"
  42. break
  43. try:
  44. print
  45. credits_str = raw_input("Enter credit hours or (Enter to quit): ")
  46. if credits_str == "":
  47. break
  48. credits = float(credits_str)
  49. print credits
  50. try:
  51. credits_int = int(credits_str)
  52. print "Error, use floating point number"
  53. print
  54. credits_str = raw_input("Enter credit hours or (Enter to quit): ")
  55. except:
  56. print credits
  57. except ValueError:
  58. print "Error, use floating point number"
  59. break
  60. stu.addGrade(grade, credits)
  61. print grade, credits
  62.  
  63. if stu.getHours() == 0.0 :
  64. print
  65. print "Zero gradepoints or credit hours recorded"
  66. print
  67. else:
  68. print "Final GPA = ", stu.gpa()
  69. print
  70.  
  71.  
  72. if __name__ == "__main__":
  73. main()

Thanks!!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Student GPA

 
0
  #2
Jan 11th, 2007
what is your error, and what is your expected output/results?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,045
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 294
woooee woooee is offline Offline
Veteran Poster

Re: Student GPA

 
0
  #3
Jan 11th, 2007
Instead of
self.hours=credits. do you want to total them
self hours += credits
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,070
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 938
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Student GPA

 
0
  #4
Jan 12th, 2007
Note that GPA is the average of the GP you enter, take a look ...
  1. print "The grade point average (GPA) calculator:"
  2.  
  3. def get_list(prompt):
  4. """
  5. loops until acceptable data or q (quit) is given
  6. returns a list of the entered data
  7. """
  8. data_list = []
  9. while True:
  10. sin = raw_input(prompt)
  11. if sin == 'q':
  12. return data_list
  13. try:
  14. data = float(sin)
  15. data_list.append(data)
  16. except ValueError:
  17. print "Enter numeric data!"
  18.  
  19. print
  20. gp_list = get_list("Enter grade point (q to quit): ")
  21. print gp_list # test
  22.  
  23. # now calculate the average (sum of items divided by total items)
  24. gpa = sum(gp_list)/len(gp_list)
  25.  
  26. print "The grade point average is:", gpa
The function get_list() is generic and can be used whenever you want to enter a series of numbers. It returns a list of the entered numbers. It's up to you to process the list of numbers.
Last edited by vegaseat; Jan 12th, 2007 at 2:44 am. Reason: fluff
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 34
Reputation: babutche is an unknown quantity at this point 
Solved Threads: 0
babutche babutche is offline Offline
Light Poster

Re: Student GPA

 
0
  #5
Jan 12th, 2007
Thank you all for helping. I have finally figured it out with your help and of course a few more mistakes of my own!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 34
Reputation: babutche is an unknown quantity at this point 
Solved Threads: 0
babutche babutche is offline Offline
Light Poster

Re: Student GPA

 
0
  #6
Jan 12th, 2007
Woooee,

Yes, that is exactly what I needed to do.! I wanted to add my credits and also my (credits * gradepoint). I also needed to indent -- stu.addGrade(grade, credits).

Thanks alot!!!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 34
Reputation: babutche is an unknown quantity at this point 
Solved Threads: 0
babutche babutche is offline Offline
Light Poster

Re: Student GPA

 
0
  #7
Jan 12th, 2007
This is the solved version of my GPA student program:

  1. import string
  2. import math
  3. class Student:
  4. def __init__(self, name, hours, qpoints):
  5. self.name = name
  6. self.hours = float(hours)
  7. self.qpoints = float(qpoints)
  8. def getName(self):
  9. return self.name
  10. def getHours(self):
  11. return self.hours
  12. def getQpoints(self):
  13. return self.qpoints
  14. def gpa(self):
  15. return self.qpoints/self.hours
  16. def addGrade(self, gradePoint, credits):
  17. self.hours += credits
  18. self.qpoints += credits*gradePoint
  19.  
  20. def main():
  21. print "This program is a modified version of the student class. It adds"
  22. print "a mutator method that records a grade and calculates the GPA for"
  23. print "the student"
  24. print
  25.  
  26.  
  27. stu = Student("stu", 0.0, 0.0)
  28. while 1:
  29. try:
  30. print
  31. grade_str = raw_input("Enter gradepoint or (Enter to quit): ")
  32. if grade_str == "":
  33. break
  34. grade = float(grade_str)
  35. try:
  36. grade_int = int(grade_str)
  37. print "Error, use floating point number"
  38. print
  39. grade_str = raw_input("Enter gradepoint or (Enter to quit): ")
  40. except:
  41. print grade
  42. except ValueError:
  43. print "Error, use floating point number"
  44. break
  45. try:
  46. print
  47. credits_str = raw_input("Enter credit hours or (Enter to quit): ")
  48. if credits_str == "":
  49. break
  50. credits = float(credits_str)
  51. try:
  52. credits_int = int(credits_str)
  53. print "Error, use floating point number"
  54. print
  55. credits_str = raw_input("Enter credit hours or (Enter to quit): ")
  56. except:
  57. print credits
  58. except ValueError:
  59. print "Error, use floating point number"
  60. break
  61. stu.addGrade(grade, credits)
  62. print grade, credits
  63.  
  64. if stu.getHours() == 0.0 :
  65. print
  66. print "Zero gradepoints or credit hours recorded"
  67. print
  68. else:
  69. print "Final GPA = ", stu.gpa()
  70. print
  71.  
  72.  
  73. if __name__ == "__main__":
  74. main()


  1. This program is a modified version of the student class. It adds
  2. a mutator method that records a grade and calculates the GPA for
  3. the student
  4.  
  5. Enter gradepoint or (Enter to quit): 3.7
  6. 3.7
  7. Enter credit hours or (Enter to quit): 3.0
  8. 3.0
  9. Enter gradepoint or (Enter to quit): 2.3
  10. 2.3
  11. Enter credit hours or (Enter to quit): 4.0
  12. 4.0
  13. Enter gradepoint or (Enter to quit): 4.0
  14. 4.0
  15. Enter credit hours or (Enter to quit): 5.0
  16. 5.0
  17. Enter gradepoint or (Enter to quit): 2.7
  18. 2.7
  19. Enter credit hours or (Enter to quit): 2.0
  20. 2.0
  21. Enter gradepoint or (Enter to quit):
  22. Final GPA = 3.26428571429


Thanks Again everyone!!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC