O.K. This is what I have changed in my program:
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, LetterGrade, credits):
self.hours = credits
self.LetterGrade = Letter
for ch in Letter:
print ord(ch)
self.qpoints = credits*Letter
if Letter == "A":
grade_str = 4.0
elif Letter == "B":
grade_str = 3.0
elif Letter == "C":
grade_str = 2.0
elif Letter == "D":
grade_str = 1.0
else:
grade_str = 0.0
return
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
print
grade = raw_input("Enter grade for next course, or nothing to finish: ")
print
credits = input("Enter the number of credit hours for this course: ")
stu = Student("stu", 0.0, 0.0)
while 1:
grade_str = raw_input("Enter grade: ")
if grade_str == "":
break
try:
grade = grade_str
except ValueError:
print "Error, use A, B, C, D or F "
return
credits_str = raw_input("Enter credits: ")
try:
credits = float(credits_str)
except ValueError:
print "Error, use floating point number"
return
stu.addLetterGrade(Letter, credits)
if stu.getHours() == 0.0:
print "Zero credit hours recorded"
else:
print "Final GPA = ", stu.gpa()
main()
But it keeps giving me this error:
Enter grade for next course, or nothing to finish: A
Enter the number of credit hours for this course: 3.0
Enter grade: B
Enter credits: 3.0
Traceback (most recent call last):
File "C:\Python23\p6extstclass.py", line 88, in ?
main()
File "C:\Python23\p6extstclass.py", line 78, in main
stu.addLetterGrade(Letter, credits)
NameError: global name 'Letter' is not defined
By the way, thank you for your help so far.