This is what I have come up with but it seems like there should be a more simple way of programming it.
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 addLetterGrade(self, letter, credits):
self.hours = credits
if letter == "A":
ngrade = 4.0
elif letter == "A-":
ngrade = 4.0
elif letter == "A+":
ngrade = 4.0
elif letter == "B":
ngrade = 3.0
elif letter == "B-":
ngrade = 3.0
elif letter == "B+":
ngrade = 3.0
elif letter == "C":
ngrade = 2.0
elif letter == "C-":
ngrade = 2.0
elif letter == "C+":
ngrade = 2.0
elif letter == "D":
ngrade = 1.0
elif letter == "D-":
ngrade = 1.0
elif letter == "D+":
ngrade = 1.0
else:
ngrade = 0.0
self.qpoints = credits * ngrade
print self.qpoints, self.hours
def main():
print
print "This program extends the modified student class program by"
print "implementing an add letter grade method"
print
stu = Student("stu", 0.0, 0.0)
done = False
while done == False:
while True:
grade_str = raw_input("Enter grade (A,A-,A+,B,B-,B+,C,C-,C+,"
"D,D-,D+,F or just "
"Enter=exit loop): ").upper()
print
if grade_str == "":
done = True
break
if grade_str not in 'A,A-,A+,B,B-,B+,C,C-,C+,D,D-,D+,F':
print "Error, use A,A-,A+,B,B-,B+,C,C-,C+,D,D-,D+,F"
else:
break
while done == False:
credits_str = raw_input("Enter credits (number of hours): ")
try:
credits = float(credits_str)
break
except ValueError:
print "Error, use floating point number"
if done == False:
stu.addLetterGrade(grade_str, credits)
if stu.getHours() == 0.0:
print "Zero credit hours recorded"
else:
print "Final GPA = ", stu.gpa()
main()
ANY Suggestions?