0

First time working with classes in python, I'm am trying to define a function that will calculate the average of 2 grades, a midterm and final exam and return the letter grade using a try and exception block. I'm very sure the issue is in the get_grade function, just looking for some guidance.

I included the entire class definition portion of my program

class StudentClass(object):

    def __init__(self, sid, name, midterm, final):
        self.id = sid
        self.name = name
        self.mid = midterm
        self.final = final

    def get_grade(self):
        try:
            average = (self.midterm + self.final) /2
            if average>=90:
                return "A"
            elif average>=80 and average <90:
                return "B"
            elif average>=70 and average <80:
                return "C"
            elif average>=60 and average <70:
                return "D"
            else:
                return "F"

        except:
            print("error")

    def getStudentData(self):
        gradeletter = self.get_grade()

        return "%-4s %-15s %5d %5d %7.1f" % (self.id, self.name, self.mid, self.final, gradeletter)

lucidbrot
  • 5,378
  • 3
  • 39
  • 68
  • 3
    "I'm very sure the issue is in the get_grade function" - what is *the issue*? – dspencer Apr 09 '20 at 13:10
  • 3
    Why do you need a try-catch at all? As long as `midterm` and `final` are numbers, there is not much that could go wrong and raise an exception. Also, in case of an exception you just swallow the exception and don't return any value from the function. – Daniel Junglas Apr 09 '20 at 13:12
  • @DanielJunglas hmmm, I think that my issue must be elsewhere in my program. Should I post full code and errors that I am getting? – user3089144 Apr 09 '20 at 13:17
  • @user3089144 You should post a [mre] of whatever the issue is. – wjandrea Apr 09 '20 at 14:03
  • BTW, a [bare `except` is bad practice](https://stackoverflow.com/q/54948548/4518341); use at least `except Exception` instead. And looking at lucidbrot's answer, you shouldn't have even used a try-except here; instead you should have let the error propagate so you could see what it was, plus the traceback showing where it occurred. – wjandrea Apr 09 '20 at 14:05

1 Answers1

0

To debug this, I'd advise you to actually have a look at the exception you're catching:

# This is a way to catch all kinds of exceptions
except Exception as e:
    print("error: {}".format(e))

Above statement will tell you, when you run a testrun with

s = StudentClass(123, "Herbert", 20, 50)
print(s.get_grade())

that the problem is

error: 'StudentClass' object has no attribute 'midterm'

And now, the problem becomes apparent: in __init__ you specify self.mid = midterm, but in your computation of the average, you use self.midterm instead of self.mid.

And in case you're curious what kind of exception that actually is, as per this answer we can check this using

print("the type of the exception is {}".format(type(e).__name__)

It turns out that this is an AttributeError.

lucidbrot
  • 5,378
  • 3
  • 39
  • 68
  • This is very helpful, but I realized that though this was an issue, there are other issues throughout my program. What would be the best way going about that? – user3089144 Apr 09 '20 at 13:30
  • You mean in terms of how to use stackoverflow for that? The general idea is that one question stays one question and if you have more questions, you pose them in different "threads" - each only in one. Ideally with a [mcve] because stripping away all the code that does NOT cause the problem often helps you find the issue. Your question here has little code, but I'm sure you would have found out yourself if you had tried to get rid of any code not needed to reproduce the problem. If my answer is helpful to you, then please do upvote and accept it :) – lucidbrot Apr 09 '20 at 13:35
  • 1
    Another way to do this type of debugging is simply remove the try-except. Then you'd get the full error message (`AttributeError: 'StudentClass' object has no attribute 'midterm'`) and traceback showing where the problem is. – wjandrea Apr 09 '20 at 14:08