Hi,

I need help please. At first I was understanding the Python language but I got lost somewhere along the line. Can someone please guide me through implementing an addLetterGrade method? I am not very good with computers and I am having a very hard time. This is my first time using a computer language so, please bare with me. I just need someone to help me and maybe answer some of my questions and explain this to me so I can figure out how to do this. Thanks!!

This is what I have so far:

``````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

##I AM STUCK HERE
self.hours = credits
for ch in Letter:
print ord(ch)
self.qpoints = credits*Letter
else:
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:
break
try:
except ValueError:
print "Error, use floating point number"
return
credits_str = raw_input("Enter credits: ")
try:
credits = float(credits_str)
except ValueError:
print "Error, use floating point number"
return

if stu.getHours() == 0.0:
print "Zero credit hours recorded"
else:
print "Final GPA = ", stu.gpa()

main()``````

## All 2 Replies

At first look, when you compare two items use == in your if statements ...

``````def addLetterGrade(self, Letter, credits):
self.hours = credits
for ch in Letter:
print ord(ch)
self.qpoints = credits*Letter
if grade_str == "A":    # use == !!!!!!!!!!!!
else:
return``````

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
self.hours = credits
for ch in Letter:
print ord(ch)
self.qpoints = credits*Letter
if Letter == "A":
elif Letter == "B":
elif Letter == "C":
elif Letter == "D":
else:
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:
break
try:
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

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 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