Skip to content

Commit 6699f9c

Browse files
authored
Update conditionals.md
add some content back in responding to Scott's comments
1 parent 9135897 commit 6699f9c

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

episodes/conditionals.md

+25-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ exercises: 10
77
::::::::::::::::::::::::::::::::::::::: objectives
88

99
- Correctly write programs that use `if` and `else` statements using Boolean expressions.
10-
- Trace the execution of un-nested conditionals and conditionals inside loops.
10+
- Trace the execution of conditionals inside of loops.
1111

1212
::::::::::::::::::::::::::::::::::::::::::::::::::
1313

@@ -54,10 +54,9 @@ for checkout in checkouts:
5454
22 is over the limit.
5555
```
5656

57-
5857
## Use `else` to execute a block of code when an `if` condition is *not* true.
5958

60-
And `else` statement can be used following `if` to allow us to specify an alternative code block to execute when the `if` *branch* is not taken.
59+
An `else` statement can be used following `if` to allow us to specify an alternative code block to execute when the `if` *branch* is not taken.
6160

6261
```python
6362
for checkout in checkouts:
@@ -84,7 +83,7 @@ You can use `elif` (short for "else if") to provide several alternative choices,
8483
```python
8584
for checkout in checkouts:
8685
if checkout > 10.0:
87-
print(f'{checkout} is over the limit.')
86+
print(f'*Warning*: {checkout} is over the limit.')
8887
elif checkout == 10:
8988
print(f'{checkout} is at the exact limit.')
9089
else:
@@ -95,11 +94,26 @@ for checkout in checkouts:
9594
0 is under the limit.
9695
3 is under the limit.
9796
10 is at the exact limit.
98-
12 is over the limit.
99-
22 is over the limit.
97+
*Warning*: 12 is over the limit.
98+
*Warning*: 22 is over the limit.
99+
100+
```
100101

102+
Conditions are tested once, in order and are not re-evaluated if values change. Python steps through the branches of the conditional in order, testing each in turn, so the order of your statements matters.
103+
104+
```python
105+
grade = 85
106+
if grade >= 70:
107+
print('grade is C')
108+
elif grade >= 80:
109+
print('grade is B')
110+
elif grade >= 90:
111+
print('grade is A')
101112
```
102113

114+
```output
115+
grade is C
116+
```
103117

104118
## Compound conditionals using `and` and `or`
105119

@@ -116,7 +130,7 @@ for user in users:
116130
for checkout in checkouts:
117131
#faculty checkout limit is 100
118132
if checkout >= 100 and user == 'fac':
119-
print(f"{checkout} is over the {user} limit.")
133+
print(f"*Warning*: {checkout} is over the {user} limit.")
120134

121135
#grad limit is 50
122136
elif checkout >= 50 and user == 'grad':
@@ -132,14 +146,13 @@ for user in users:
132146
```output
133147
3 is under the fac limit.
134148
50 is under the fac limit.
135-
120 is over the fac limit.
149+
*Warning*: 120 is over the fac limit.
136150
137151
3 is under the grad limit.
138-
50 is over the grad limit.
139-
120 is over the grad limit.
152+
*Warning*: 50 is over the grad limit.
153+
*Warning*: 120 is over the grad limit.
140154
```
141155

142-
143156
::::::::::::::::::::::::::::::::::::::: challenge
144157

145158
## Age conditionals
@@ -279,6 +292,7 @@ for filename in glob.glob('data/*.csv'):
279292
- Conditionals are often used inside loops.
280293
- Use `else` to execute a block of code when an `if` condition is *not* true.
281294
- Use `elif` to specify additional tests.
295+
- Conditions are tested once, in order.
282296
- Use `and` and `or` to check against multiple value statements.
283297

284298
::::::::::::::::::::::::::::::::::::::::::::::::::

0 commit comments

Comments
 (0)