Question

Python 3.7+. Any better way to write this code? This is a method from my Student...

Python 3.7+. Any better way to write this code? This is a method from my Student class:

# method to calculate GPA
def gpa(self):

try:
    return self.qpoints/self.credits
except ZeroDivisionError :
    return 0

I'd rather not use try/except, and I would also rather not use ZeroDivisionError. Is there a better way to write this? Maybe an if/else statement?

0 0
Add a comment Improve this question Transcribed image text
Answer #1
# method to calculate GPA
def gpa(self):
    if self.credits==0:
        return 0
    else:
        return self.qpoints/self.credits
        
Add a comment
Know the answer?
Add Answer to:
Python 3.7+. Any better way to write this code? This is a method from my Student...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT