Check your understanding of the previous reading: if/else syntax, branching, and tracing a program that skips lines.
In the following code, the print statement is ____ the if statement:
if conditional:
print("The conditional was true")
Select one answer, then check.
if, unindented.print line is indented, so it is part of the if statement's body.if statement, unindented.How many branches does this code have?
if conditional:
print(5)
print(2)
Select one answer, then check.
if statement, so there is at least one branch point.if statements.conditional is true (runs print(5) then print(2)) or it is false (runs only print(2)) — two possible routes through the code.if with no elif/else only ever splits into two routes.How many branches does this code have?
if conditional:
if second_conditional:
print(5)
print(1)
print(2)
Select one answer, then check.
if statements here, so there is more than one branch point.conditional and second_conditional.if only runs when conditional is true.conditional false (only print(2)); conditional true and second_conditional false (print(1), print(2)); conditional true and second_conditional true (print(5), print(1), print(2)).How many branches does this code have?
if conditional:
print(5)
else:
print(2)
Select one answer, then check.
if/else always has at least one branch point.conditional is true, and what happens when it is false?conditional is true (runs print(5)) or false (runs print(2)) — exactly one of the two bodies always runs.Trace this program line by line. Fill in salary and taxes after each step; put an X (without quotes) if a value is not yet defined.
1 | salary = 1000
2 | if salary > 100:
3 | taxes = .5
4 | else:
5 | taxes = .1
6 | salary = taxes * salary
7 | if salary > 100:
8 | if salary < 500:
9 | taxes = 0
10 | salary = taxes * salary
A step can repeat a line's values unchanged when that line is only the if/elif/else keyword itself — the condition is checked, but nothing is assigned.
| Step | Line | salary | taxes |
|---|---|---|---|
| 1 | 1 | 1000 | |
| 2 | 2 | 1000 | |
| 3 | 1000 | ||
| 4 | 6 | ||
| 5 | |||
| 6 | |||
| 7 |
Step 1 assigns salary = 1000; taxes does not exist yet, so it stays X.
Step 2 checks salary > 100 (true), but a line with just if assigns nothing, so both variables are unchanged.
Step 3 runs the if body: taxes = .5.
Step 4 is line 6: salary = taxes * salary is .5 * 1000 = 500.0; taxes stays .5.
Step 5 is line 7: salary > 100 (500 > 100) is true, nothing is assigned.
Step 6 is line 8: salary < 500 (500 < 500) is false, so line 9 is skipped entirely; nothing is assigned.
Step 7 is line 10: salary = taxes * salary is .5 * 500.0 = 250.0; taxes stays .5 since line 9 never ran.
Go line by line, not statement by statement. A line that only contains if ...:, elif ...: or else: is still its own step, but it never changes a variable's value — it only decides which line runs next.
What is the value of number_of_users after this program executes?
if False:
number_of_users = 5
print(number_of_users)
Select one answer, then check.
False, the assignment on line 2 never runs, so number_of_users was never created; print raises a NameError.None; a name that was never assigned does not exist.if False: body, so it never runs.