| | |
Student GPA
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 34
Reputation:
Solved Threads: 0
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?
Thanks!!
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?
Python Syntax (Toggle Plain Text)
import string import math class Student: def __init__(self, name, hours, qpoints): self.name = name self.hours = float(hours) self.qpoints = float(qpoints) def getName(self): return self.name def getHours(self): return self.hours def getQpoints(self): return self.qpoints def gpa(self): return self.qpoints/self.hours def addGrade(self, gradePoint, credits): self.hours = credits self.qpoints = credits*gradePoint def main(): print "This program is a modified version of the student class. It adds" print "a mutator method that records a grade and calculates the GPA for" print "the student" stu = Student("stu", 0.0, 0.0) while 1: try: grade_str = raw_input("Enter gradepoint or (Enter to quit): ") if grade_str == "": break grade = float(grade_str) try: grade_int = int(grade_str) print "Error, use floating point number" grade_str = raw_input("Enter gradepoint or (Enter to quit): ") except: print grade except ValueError: print "Error, use floating point number" break try: credits_str = raw_input("Enter credit hours or (Enter to quit): ") if credits_str == "": break credits = float(credits_str) print credits try: credits_int = int(credits_str) print "Error, use floating point number" credits_str = raw_input("Enter credit hours or (Enter to quit): ") except: print credits except ValueError: print "Error, use floating point number" break stu.addGrade(grade, credits) print grade, credits if stu.getHours() == 0.0 : print "Zero gradepoints or credit hours recorded" else: print "Final GPA = ", stu.gpa() if __name__ == "__main__": main()
Thanks!!
Note that GPA is the average of the GP you enter, take a look ...
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.
python Syntax (Toggle Plain Text)
print "The grade point average (GPA) calculator:" def get_list(prompt): """ loops until acceptable data or q (quit) is given returns a list of the entered data """ data_list = [] while True: sin = raw_input(prompt) if sin == 'q': return data_list try: data = float(sin) data_list.append(data) except ValueError: print "Enter numeric data!" gp_list = get_list("Enter grade point (q to quit): ") print gp_list # test # now calculate the average (sum of items divided by total items) gpa = sum(gp_list)/len(gp_list) print "The grade point average is:", gpa
Last edited by vegaseat; Jan 12th, 2007 at 2:44 am. Reason: fluff
May 'the Google' be with you!
•
•
Join Date: Oct 2006
Posts: 34
Reputation:
Solved Threads: 0
This is the solved version of my GPA student program:
Thanks Again everyone!!!
Python Syntax (Toggle Plain Text)
import string import math class Student: def __init__(self, name, hours, qpoints): self.name = name self.hours = float(hours) self.qpoints = float(qpoints) def getName(self): return self.name def getHours(self): return self.hours def getQpoints(self): return self.qpoints def gpa(self): return self.qpoints/self.hours def addGrade(self, gradePoint, credits): self.hours += credits self.qpoints += credits*gradePoint def main(): print "This program is a modified version of the student class. It adds" print "a mutator method that records a grade and calculates the GPA for" print "the student" stu = Student("stu", 0.0, 0.0) while 1: try: grade_str = raw_input("Enter gradepoint or (Enter to quit): ") if grade_str == "": break grade = float(grade_str) try: grade_int = int(grade_str) print "Error, use floating point number" grade_str = raw_input("Enter gradepoint or (Enter to quit): ") except: print grade except ValueError: print "Error, use floating point number" break try: credits_str = raw_input("Enter credit hours or (Enter to quit): ") if credits_str == "": break credits = float(credits_str) try: credits_int = int(credits_str) print "Error, use floating point number" credits_str = raw_input("Enter credit hours or (Enter to quit): ") except: print credits except ValueError: print "Error, use floating point number" break stu.addGrade(grade, credits) print grade, credits if stu.getHours() == 0.0 : print "Zero gradepoints or credit hours recorded" else: print "Final GPA = ", stu.gpa() if __name__ == "__main__": main()
Python Syntax (Toggle Plain Text)
This program is a modified version of the student class. It adds a mutator method that records a grade and calculates the GPA for the student Enter gradepoint or (Enter to quit): 3.7 3.7 Enter credit hours or (Enter to quit): 3.0 3.0 Enter gradepoint or (Enter to quit): 2.3 2.3 Enter credit hours or (Enter to quit): 4.0 4.0 Enter gradepoint or (Enter to quit): 4.0 4.0 Enter credit hours or (Enter to quit): 5.0 5.0 Enter gradepoint or (Enter to quit): 2.7 2.7 Enter credit hours or (Enter to quit): 2.0 2.0 Enter gradepoint or (Enter to quit): Final GPA = 3.26428571429
Thanks Again everyone!!!
![]() |
Similar Threads
- Unsorted List ADT Client Code Help (C++)
- Structures (C++)
- Loop Program Wont work right..Help me please! (C)
- Urgent help needed (Database Design)
- Someone Be My Program Study Buddy (C++)
- grades.java -- help!! (Java)
- need help to debugg this code please! (C++)
Other Threads in the Python Forum
- Previous Thread: min/max of a mixed type list
- Next Thread: Show a Dice Roll (Python and Tk)
| Thread Tools | Search this Thread |
abrupt ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog data decimals dictionaries dictionary drive dynamic error examples excel exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyqt python random recursion schedule script scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






