FAQ: Control Flow - Else Statements

This community-built FAQ covers the “Else Statements” exercise from the lesson “Control Flow”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Else Statements

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

4 posts were split to a new topic: Why can’t else go in the middle of different ifs?

Is it possible to write if else or if elif else statements on one line as shown below:

if (criteria): dosomethingInIf
elif (criteria): dosomethingInElIf
else: dosomethingElse

Thanks in advance

The answer to exercise 1 in “10. Else Statments”, goes like this:

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."

I dotn’t unserdant to which “if” is this “else” at the end related. If I’m saying that the person does not meet the gpa OR the credtis, why I should use else after the “if” that checks “not gpa > 2.0 and credits >= 120”? It shouldn’t be more logical to use it after “if (gpa >= 2.0) and (credits >= 120)”? Since I know that, if this last statment is false, then for sure GPA or credtis requirements are not being met.

The else statement is invariably connected the immediately preceding if statement; in this case: if not (gpa >= 2.0) and (credits >= 120):. The series of if statements is to ensure the returned string is the most useful information possible. Perhaps it’d read a little better with a chain of if / elif style statements but I’d not worry about it too much for this lesson. You’re looking to learn control flow for now, a lot of the coding will sometimes seem clunky and odd to try and keep it limited to concepts already introduced.

It’s fair to say the language used in that string is perhaps a little ambiguous (or at least for those of us who aren’t grammar experts such as myself) but for me personally that reads as an inclusive or in English (“you do not meet the GPA and you do not meet the credit requirement”). Potentially rephrased with a little less ambiguity as: “You meet neither the required GPA nor the credit requirements for graduation”. It may read that way entirely due to the context in which it is placed though so I’d always advocate for less ambiguity.

Were it placed immediately following the first if statement then that particular string wouldn’t quite make sense in my head: “You do not meet the GPA or the credit requirement for graduation.”. And if it were an exclusive statement I’d personally rephrase it as: “You do not meet the GPA requirements or you do not meet the credit requirements”, which seems less ambiguous to me.

That’s at least how it read to me and why using the return only when the other possibilities had been exhausted made more sense.

Since the requirements state the exact string “If a student is failing to meet both graduation requirements” then to return that string then that’s what you’ll need to do regardless of the contents of the string. Just act like it’s a client’s request and get it done. When it comes to sections where you can do your own projects and make your own choices then you can make it nice and clear and wholly unambiguous.

2 Likes

Thank you very much, it was an amazing answer. It will help me way beyond the limits of the question I have just made.

1 Like