As you can tell from your work with Calvin Coolidge’s Cool College, once you start including lots of
if statements in a function the code becomes a little cluttered and clunky. Luckily, there are other tools we can use to build control flow.
else statements allow us to elegantly describe what we want our code to do when certain conditions are not met.
else statements always appear in conjunction with
if statements. Consider our waking-up example to see how this works:
if weekday: wake_up("6:30") else: sleep_in()
In this way, we can build if statements that execute different code if conditions are or are not met. This prevents us from needing to write
if statements for each possible condition, we can instead write a blanket
else statement for all the times the condition is not met.
Let’s return to our
age_check function for our movie streaming platform. Previously, all it did was check if the user’s age was over
13 and if so return
True. We can use an
else statement to return a message in the event the user is too young to watch the movie.
def age_check(age): if age >= 13: return True else: return "Sorry, you must be 13 or older to watch this movie."
Calvin Coolidge’s Cool College has another request for you. They want you to add an additional check to the
graduation_reqs function. 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."
else statement to add this to your function.