Hi,

I am trying to average student GPA. I thought I had this problem solved but then I ran it and the GPA did not come out correct. Can anyone give any suggestions?

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 addGrade(self, gradePoint, credits):
        self.hours = credits
        self.qpoints = credits*gradePoint
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
    
    stu = Student("stu", 0.0, 0.0)
    while 1:
        try:
            print
            grade_str = raw_input("Enter gradepoint or (Enter to quit): ")
            if grade_str == "":
                break
            grade = float(grade_str)
            try:
                grade_int = int(grade_str)
                print "Error, use floating point number"
                print
                grade_str = raw_input("Enter gradepoint or (Enter to quit): ")    
            except:
                print grade
        except ValueError:
            print "Error, use floating point number"
            break    
        try:
            print
            credits_str = raw_input("Enter credit hours or (Enter to quit): ")
            if credits_str == "":
                break
            credits = float(credits_str)
            print credits
            try:
                credits_int = int(credits_str)
                print "Error, use floating point number"
                print
                credits_str = raw_input("Enter credit hours or (Enter to quit): ")
            except:
                print credits
        except ValueError:
            print "Error, use floating point number"
            break 
        stu.addGrade(grade, credits)
        print grade, credits
                   
    if stu.getHours() == 0.0 :
        print
        print "Zero gradepoints or credit hours recorded"
        print
    else:
        print "Final GPA = ", stu.gpa()
        print
    
        
if __name__ == "__main__":
    main()

Thanks!!

what is your error, and what is your expected output/results?

Instead of
self.hours=credits. do you want to total them
self hours += credits

Note that GPA is the average of the GP you enter, take a look ...

print "The grade point average (GPA) calculator:"
 
def get_list(prompt):
    """
    loops until acceptable data or q (quit) is given
    returns a list of the entered data
    """
    data_list = []
    while True:
        sin = raw_input(prompt)
        if sin == 'q':
            return data_list
        try:
            data = float(sin)
            data_list.append(data)
        except ValueError:
            print "Enter numeric data!"
 
print
gp_list = get_list("Enter grade point (q to quit): ")
print gp_list  # test
 
# now calculate the average (sum of items divided by total items)
gpa = sum(gp_list)/len(gp_list)
 
print "The grade point average is:", gpa

The function get_list() is generic and can be used whenever you want to enter a series of numbers. It returns a list of the entered numbers. It's up to you to process the list of numbers.

Thank you all for helping. I have finally figured it out with your help and of course a few more mistakes of my own!

Woooee,

Yes, that is exactly what I needed to do.! I wanted to add my credits and also my (credits * gradepoint). I also needed to indent -- stu.addGrade(grade, credits).

Thanks alot!!!

This is the solved version of my GPA student 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 addGrade(self, gradePoint, credits):
        self.hours += credits
        self.qpoints += credits*gradePoint
        
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
    
    
    stu = Student("stu", 0.0, 0.0)
    while 1:
        try:
            print
            grade_str = raw_input("Enter gradepoint or (Enter to quit): ")
            if grade_str == "":
                break
            grade = float(grade_str)
            try:
                grade_int = int(grade_str)
                print "Error, use floating point number"
                print
                grade_str = raw_input("Enter gradepoint or (Enter to quit): ")    
            except:
                print grade
        except ValueError:
            print "Error, use floating point number"
            break    
        try:
            print
            credits_str = raw_input("Enter credit hours or (Enter to quit): ")
            if credits_str == "":
                break
            credits = float(credits_str)
            try:
                credits_int = int(credits_str)
                print "Error, use floating point number"
                print
                credits_str = raw_input("Enter credit hours or (Enter to quit): ")
            except:
                print credits
        except ValueError:
            print "Error, use floating point number"
            break 
        stu.addGrade(grade, credits)
        print grade, credits
                   
    if stu.getHours() == 0.0 :
        print
        print "Zero gradepoints or credit hours recorded"
        print
    else:
        print "Final GPA = ", stu.gpa()
        print
    
        
if __name__ == "__main__":
    main()
This program is a modified version of the student class. It adds
a mutator method that records a grade and calculates the GPA for
the student

Enter gradepoint or (Enter to quit): 3.7
3.7
Enter credit hours or (Enter to quit): 3.0
3.0
Enter gradepoint or (Enter to quit): 2.3
2.3
Enter credit hours or (Enter to quit): 4.0
4.0
Enter gradepoint or (Enter to quit): 4.0
4.0
Enter credit hours or (Enter to quit): 5.0
5.0
Enter gradepoint or (Enter to quit): 2.7
2.7
Enter credit hours or (Enter to quit): 2.0
2.0
Enter gradepoint or (Enter to quit): 
Final GPA =  3.26428571429

Thanks Again everyone!!!

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.