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?
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"
print
stu = Student("stu", 0.0, 0.0)
while 1:
try:
print
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"
print
grade_str = raw_input("Enter gradepoint or (Enter to quit): ")
except:
print grade
except ValueError:
print "Error, use floating point number"
break
try:
print
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"
print
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
print "Zero gradepoints or credit hours recorded"
print
else:
print "Final GPA = ", stu.gpa()
print
if __name__ == "__main__":
main()
Thanks!!