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.

Recommended Answers

All 7 Replies

Two issues here, one is the use of code tags in your posting of code. The tags are particularly important with Python code, since they preserve the indentations. Without those indentation much of the code becomes unreadable, as the statement blocks can not clearly be identified. The second issue, you seemed to have lost all indentations even in your raw posting. What kind of edtor are you using?

I am using IDLE. I did not know exactlywhat what you meant by code tags. I'm sorry I copied the program wrong earlier.

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
 
LET ME ASK A QUESTION RIGHT HERE. ARE SELF, LETTERGRADE AND CREDITS OBJECTS?
 
    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()

For the proper use of code tags see:
http://www.daniweb.com/techtalkforums/announcement114-3.html

O.K. I read the link on code tags and now I understand! Thanks!

I went through some of your code and corrected a number of things ...

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
 
#LET ME ASK A QUESTION RIGHT HERE. ARE SELF, LETTERGRADE AND CREDITS OBJECTS?
# the answer is yes, yes, yes
 
    def addLetterGrade(self, letter, credits):  # changed LetterGrade to letter !!!!
        self.hours = credits
        # why?  just keep variable letter
        self.LetterGrade = letter
        # why?
        for ch in letter:
            print ord(ch)
        # if you multiply a number with a string you get a multiple string!!!!
        #self.qpoints = credits * Letter
        if letter == "A":
            ngrade = 4.0
        elif letter == "B":
            ngrade = 3.0
        elif letter == "C":
            ngrade = 2.0
        elif letter == "D":
            ngrade = 1.0
        else:
            ngrade = 0.0
        # now you are mutliplying numbers!!!!
        self.qpoints = credits * ngrade
        print self.qpoints, self.hours  # test
        
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: ")  # where is grade used????
    #print
    #credits = input("Enter the number of credit hours for this course: ")  # variable credits is in the loop later
    
    stu = Student("stu", 0.0, 0.0)
    
    # some major corrections here in the input loops!!!!
    done = False
    while done == False:
        while True:
            # this will make all entries upper case
            grade_str = raw_input("Enter grade (A, B, C, D, F or just Enter=exit loop): ").upper()
            if grade_str == "":
                # breaks out of the enter grade loop
                done = True
                break
            # this is the way you do it!!!!
            if grade_str not in 'ABCDF':
                print "Error, use A, B, C, D or F"
            else:
                # breaks out of the enter grade loop
                break
        while done == False:
            credits_str = raw_input("Enter credits (number of hours): ")
            try:
                credits = float(credits_str)
                # breaks out of the enter credits loop
                break
            except ValueError:
                print "Error, use floating point number"
                # use return only in a function!!!!
                #return
        if done == False:
            stu.addLetterGrade(grade_str, credits)  # use grade_str (variable Letter is not known)!!!!
                   
    # comparing floating point numbers directly is
    # tricky because of the round-off errors!!!!
    # might use something like --> if stu.getHours() < 0.05:
    if stu.getHours() == 0.0:
        print "Zero credit hours recorded"
    else:
        # gpa is really not properly calculated
        # you only get the gpa of your final entry!!!!
        print "Final GPA = ", stu.gpa()
    
    
        
main()

I went through some of your code and corrected a number of things ...

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
 
#LET ME ASK A QUESTION RIGHT HERE. ARE SELF, LETTERGRADE AND CREDITS OBJECTS?
# the answer is yes, yes, yes
 
    def addLetterGrade(self, letter, credits):  # changed LetterGrade to letter !!!!
        self.hours = credits
        # why?  just keep variable letter
 
#I AM CONFUSED RIGHT HERE. SHOULD IT BE:
       
 self.letter = letter
 
#IS THIS WHAT YOU ARE REFERRING TO?
        
self.LetterGrade = letter
        # why?
        for ch in letter:
            print ord(ch)
        # if you multiply a number with a string you get a multiple string!!!!
        #self.qpoints = credits * Letter
 
#I AM CONFUSED FROM THIS POINT UP TO self.hours = credits
 
#O.K. I AM ASUMING THAT I DO NOT NEED THE
--- for ch in letter:
          print ord(ch)
 
#BECAUSE I AM CHANGING FROM CHARACTERS TO NUMERIC 
 
#BELOW. RIGHT?
        
        if letter == "A":
            ngrade = 4.0
        elif letter == "B":
            ngrade = 3.0
        elif letter == "C":
            ngrade = 2.0
        elif letter == "D":
            ngrade = 1.0
        else:
            ngrade = 0.0
        # now you are mutliplying numbers!!!!
        self.qpoints = credits * ngrade
        print self.qpoints, self.hours  # test
 
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: ")  # where is grade used????
    #print
    #credits = input("Enter the number of credit hours for this course: ")  # variable credits is in the loop later
 
    stu = Student("stu", 0.0, 0.0)
 
    # some major corrections here in the input loops!!!!
    done = False
    while done == False:
        while True:
            # this will make all entries upper case
            grade_str = raw_input("Enter grade (A, B, C, D, F or just Enter=exit loop): ").upper()
            if grade_str == "":
                # breaks out of the enter grade loop
                done = True
                break
            # this is the way you do it!!!!
            if grade_str not in 'ABCDF':
                print "Error, use A, B, C, D or F"
            else:
                # breaks out of the enter grade loop
                break
        while done == False:
            credits_str = raw_input("Enter credits (number of hours): ")
            try:
                credits = float(credits_str)
                # breaks out of the enter credits loop
                break
            except ValueError:
                print "Error, use floating point number"
                # use return only in a function!!!!
                #return
        if done == False:
 
#I NEED TO ASK MORE QUESTIONS HERE. IS THIS WHAT IS CALLED
#"CALLING THE METHOD" 
#AND
#OBJECTS ARE: grade_str and credits?
            stu.addLetterGrade(grade_str, credits)  # use grade_str (variable Letter is not known)!!!!
 
    # comparing floating point numbers directly is
    # tricky because of the round-off errors!!!!
    # might use something like --> if stu.getHours() < 0.05:
    if stu.getHours() == 0.0:
        print "Zero credit hours recorded"
    else:
        # gpa is really not properly calculated
        # you only get the gpa of your final entry!!!!
        print "Final GPA = ", stu.gpa()
 
 
 
main()

I AM HAVING A HARD TIME UNDERSTANDING CLASSES.
THANK YOU!!

I looked at some of your questions.

#I AM CONFUSED RIGHT HERE. SHOULD IT BE:
 self.letter = letter

Since you only going to use the variable 'letter' in this particular method, you can keep it local, no need to add the 'self.' prefix.

for ch in letter:
            print ord(ch)

The function ord() gives you the ASCII value for the character, for instance 'A' would give you the integer 65.

stu.addLetterGrade(grade_str, credits)

Yes you are calling a method of the class instance 'stu'

You really wouldn't need a class for a simple program like this, but if you had a number of students you could create an instance for each, something like:

bill = Student("Bill Bunker", 0.0, 0.0)
tom = Student("Tom Tinker", 0.0, 0.0)
henry = Student("Hank Prince", 0.0, 0.0)

An extension of your program, so each student has his/her own class instance that could be saved with all the data when you are done.

Thank You Very Much For All Of Your Help!! Between This Site And The Tutorials I Have Read I Am Starting To Understand Classes A Little Bit Beter.

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.