Hi,

I did get the problem fixed with my "break" not working properly but now my ValueError will not work properly. If you enter a number that is not a floating number it should create a value error(as a matter of fact it did before I fixed my other problem ("break"). Can anyone help, please! Thanks!!!

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:
        print
        grade_str = raw_input("Enter gradepoint or (Enter to quit): ")
        if grade_str == "":
            break
        try:
            grade = float(grade_str)
        except ValueError:
            print "Error, use floating point number"
            return
        print
        credits_str = raw_input("Enter credit hours or (Enter to quit): ")
        if credits_str == "":
            break
        try:
            credits = float(credits_str)
        except ValueError:
            print "Error, use floating point number"
            return
        stu.addGrade(grade, credits)
 
    if stu.getHours() == 0.0 :
        print
        print "Zero gradepoints or credit hours recorded"
        print
    else:
        print "Final GPA = ", stu.gpa()
        print

Thank You!!

The return statement will not only break out of the while loop but also out of function main()!!

If you enter data in a loop you need to accumulate the entries in some fashion.

def main():
        grade_str = raw_input("Enter gradepoint or (Enter to quit): ")
        if grade_str == "":
            break
        try:
            grade = float(grade_str)
        except ValueError:
            print "Error, use floating point number"
            return

You want the "try :" statement as the first statement after "while 1:". That will give you an error message if a number is not entered. If an integer is entered, it can legally be type cast into a float. If a float is entered and you try to convert it into an integer you will get an error, so inserting a "grade_int = int(grade_str) and another try:/except: statement will do the trick, but it seems cumbersome. You could also use divmod(grade_float, 1) and check for a remainder. Can someone legimately have a gpa of 70? If so you want to be able to enter an integer. If the gpa is entered as 0.70, then you want to test that the float is less than one.

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"
        except :
           print grade
     except ValueError:
            print "Error, use floating point number"

Edit: Added additional try/except statement. Sorry, I did the first post in a hurry.
Edit-2: I would suggest that you have a GetInput function that you would pass "grade point" or "credit hours" to, it would get the input and convert to floating point and return the number or a -1 if bad data and then exit with an 'enter'. You are doing the same thing twice, so putting this is in a function is more efficient and breaks the code up into smaller chunks, which is easier to debug. Looks like you are just about finished though. Any plans for a Tkinter or other GUI interface?

Thank You for your help. I had tried to put another "try, except" statement in my program but I didn't do it right. I never really thought about the "int(grade_string)".

Right now I do not have any plans for any Tinker or other GUI interface. But I have done it before. I thought it was very hard and confusing. Of course, when I started the section in the book on classes, I found it even more difficult!! But I am learning with each problem that I finish. I am currently taking an online class to learn Python and I must say that this web site has been a tremendous help! It is like having an instuructor that is online all of the time. You guys (or gals) really know what you're doing and you explain what is going on very well. I really appreciate your time! Thanks again!

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.