How to do it better? including not

def graduation_reqs(gpa, credits):
if (gpa >= 2.0) and (credits >= 120):
return “You meet the requirements to graduate!”
if (gpa >= 2.0) and (credits < 120):
return “You do not have enough credits to graduate.”
if (gpa < 2.0) and (credits >=120):
return “Your GPA is not high enough to graduate.”
if (gpa < 2.0) and (credits < 120):
return “You do not meet either requirement to graduate!”

if gpa > 1.9
    if credits > 119
        return "graduate"
    else 
        return "not enough credits"
else 
    return "You do not meet requirements"
1 Like

wow thats simple. Doesn’t cover gpa shortage. Also wouldn’t this be better with elif?

You can change the last line to say “Bad GPA” it would logically mean that.

No else if is needed

There are two if/else statements nested.

if your gpa is high enough then your credits get evaluated otherwise you have a bad gpa
If your credits are high enough you graduate otherwise you don’t have enough credits

I would change the gpa > 1.9 to gpa >=2.0 in retrospect
Otherwise a 1.91 student could graduate

1 Like