Check your understanding of the previous reading: the six logical patterns, and how to reason about multiple return statements.
if statement with an and can be replaced with two if statements, one inside the other.if A and B: is the same as if A: if B:.if statement so that errors are not caused in certain conditions.ZeroDivisionError or a bad conversion before it happens.if/elif/else branches by putting return statements in each branch.return inside each branch.if/elif/else branches by updating a variable and then only having one return statement at the end.result variable in each branch, then one return result at the end.else branches to specify default behavior by assigning an initial value to a variable prior to the if statements.What happens when there are multiple return statements in a function?
Select every answer that applies, then check.
return ends the function call, not the whole program; execution continues right after the call.return executes, the function is over — nothing after it runs for that call.return statements as you like, as long as at most one of them executes per call.return that actually executes matters; a function ends the moment it runs.return executes per call, so there is nothing to combine.return unconditionally ends the function, so any code written right after it at the same indentation — including another return — is unreachable. if/elif/else branches are what let several return statements each be reachable.Why might you use the Build-Up-Return pattern instead of the Multiple-Returns pattern?
Select every answer that applies, then check.
return to a single spot makes the control flow easier to reason about.return result, which is usually about the same length as Multiple Return Spots.if/elif/else chain (ending in else) to guarantee a value — forgetting a branch is just as possible either way.In which of these cases might you need a Defensive Guard to avoid an error?
Select every answer that applies, then check.
distance / time with if time > 0:.int(user_input) with if user_input.isdigit():.str() works on any integer — positive, negative, or zero — without error.not works on any boolean value without error.""[0]) raises an IndexError, so checking the string is non-empty first avoids it.some_string[:10] never raises an error, even on a shorter string — it just returns however many characters are available.Which of these comparisons between the patterns are accurate?
Select every answer that applies, then check.
if statement ending a branch inside of a function before an error ... can occur."or at all — its examples are plain if checks.else at every level of nested if, not with Multiple Return Spots — the reading never draws that comparison.What does the following code print?
alpha = 20
beta = 3
if alpha > 10:
if beta > 10:
alpha = 10
print(alpha)
The outer condition alpha > 10 is true (20 > 10), so Python enters that block. But the inner condition beta > 10 is false, since beta is 3. So the inner if body (alpha = 10) never runs, and alpha keeps its original value. print(alpha) prints 20.
Given the following code:
gamma = "Hello!"
if gamma and gamma[-1] == "!":
print("Exclaim")
Which of the following programs are functionally equivalent to the conditional part, for any string value of gamma (not just "Hello!")?
Select every answer that applies, then check.
len(gamma) > 0 is true exactly when gamma is truthy, and and short-circuits the same way in both versions, so an empty string never reaches the indexing.or, any non-empty gamma makes the condition true regardless of its last character, and an empty gamma would fall through to gamma[-1] and raise an IndexError — the original never does either."!", and still indexes gamma[-1] unguarded when gamma is empty.gamma prints via the first branch, regardless of its last character — the original also requires the last character to be "!".