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
    def addLetterGrade(self, Letter, credits):
        self.hours = credits
        for ch in Letter:
            print ord(ch)
        self.qpoints = credits*Letter
        if grade_str = "A":
            grade_str = 4.0
        elif grade_str = "B":
            grade_str = 3.0
        elif grade_str = "C":
            grade_str = 2.0
        elif grade_str = "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 = float(grade_str)
        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
        stu.addGrade(grade, credits)
                   
    if stu.getHours() == 0.0:
        print "Zero credit hours recorded"
    else:
        print "Final GPA = ", stu.gpa()
    
    
        
main()

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 == !!!!!!!!!!!!
            grade_str = 4.0
        elif grade_str == "B":
            grade_str = 3.0
        elif grade_str == "C":
            grade_str = 2.0
        elif grade_str == "D":
            grade_str = 1.0
        else:
            grade_str = 0.0
            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
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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.