Why can't else go in the middle of different ifs?

[https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-control-flow/lessons/python-control-flow/exercises/else-statements]

Hi there ! Regarding the next code:

def graduation_reqs(gpa, credits):
  if (gpa >= 2.0) and (credits >= 120):
    return "You meet the requirements to graduate!"
  else:
    return "You do not meet the GPA or the credit requirement for graduation."
  
  if (gpa >= 2.0) and not (credits >= 120):
    return "You do not have enough credits to graduate."
  if not (gpa >= 2.0) and (credits >= 120):
    return "Your GPA is not high enough to graduate."

Why can’t I use “else” after the “if” statement i’m interested in ? The instructions says: " If a student is failing to meet both graduation requirements, they want the function to return:

“You do not meet the GPA or the credit requirement for graduation.”

Use an else statement to add this to your function."

The correct code according to the tutorial is the one with the “else” statement after all the “if” statements, at the very end.
Thank you.

Here’s the desired output (truth table) for this function:

Condition set:                           Output:

gpa >= 2.0           "You meet the requirements to graduate!"
credits >= 120

gpa >= 2.0           "You do not have enough credits to graduate."
credits < 120

gpa < 2.0            "You do not have enough credits to graduate."
credits >= 120

gpa < 2.0            "You do not meet the GPA or the credit requirement for graduation."
credits < 120

Your version does not return those outputs. For example:

print(graduation_reqs(1.0, 130)) # Third condition set, above

Output:

You do not meet the GPA or the credit requirement for graduation.

1 Like

Hello,

I had the same question about this while carrying out the exercise. I believe that if you place the else statement right under the first if statement, the else statement will be carried out before any of the further if statements. That’s why the else statement should come at the end.

10 Likes

if -- elif -- elif ... -- else

… should be considered one linked chain. Each link has a condition and a block (indented sequence of statements). If the condition returns False, then the block does not execute, and control passes to the next link.

But if a condition returns True, the block does execute, and all further links (including else) are bypassed. Control then passes to the next line of code following the chain.

6 Likes

If i write the fuction like the right answer, so:

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

With the following print statement:

Blockquote
print(graduation_reqs(3.0, 100))
< Blockquote

it prints out “You do not have enough credits to graduate.”

Otherwise, if i print this with the function by the questioner, i get the right output. But of course, if just one boolean variable is false, i get everytime just You do not meet the GPA or the credit requirement for graduation.

Thats confusing.

The OP’s code’s 2nd and 3rd if’s never run because either first if statement or else will execute and with return statement we end up function execution.

1 Like

Hi Patrick, thanks for this!

Just wanted to point out that for lines 2 and 3 of your code, you have the same output for both conditions. Should not line 3 output Your GPA is not high enough to graduate?

gpa >= 2.0           "You do not have enough credits to graduate."
credits < 120

gpa < 2.0            "You do not have enough credits to graduate."
credits >= 120
Summary

This text will be hidden

Agreed. Thanks for pointing that out!