You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Use `else` to execute a block of code when an `if` condition is *not* true.
59
58
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.
61
60
62
61
```python
63
62
for checkout in checkouts:
@@ -84,7 +83,7 @@ You can use `elif` (short for "else if") to provide several alternative choices,
84
83
```python
85
84
for checkout in checkouts:
86
85
if checkout >10.0:
87
-
print(f'{checkout} is over the limit.')
86
+
print(f'*Warning*: {checkout} is over the limit.')
88
87
elif checkout ==10:
89
88
print(f'{checkout} is at the exact limit.')
90
89
else:
@@ -95,11 +94,26 @@ for checkout in checkouts:
95
94
0 is under the limit.
96
95
3 is under the limit.
97
96
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
+
```
100
101
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')
101
112
```
102
113
114
+
```output
115
+
grade is C
116
+
```
103
117
104
118
## Compound conditionals using `and` and `or`
105
119
@@ -116,7 +130,7 @@ for user in users:
116
130
for checkout in checkouts:
117
131
#faculty checkout limit is 100
118
132
if checkout >=100and user =='fac':
119
-
print(f"{checkout} is over the {user} limit.")
133
+
print(f"*Warning*: {checkout} is over the {user} limit.")
120
134
121
135
#grad limit is 50
122
136
elif checkout >=50and user =='grad':
@@ -132,14 +146,13 @@ for user in users:
132
146
```output
133
147
3 is under the fac limit.
134
148
50 is under the fac limit.
135
-
120 is over the fac limit.
149
+
*Warning*: 120 is over the fac limit.
136
150
137
151
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.
140
154
```
141
155
142
-
143
156
::::::::::::::::::::::::::::::::::::::: challenge
144
157
145
158
## Age conditionals
@@ -279,6 +292,7 @@ for filename in glob.glob('data/*.csv'):
279
292
- Conditionals are often used inside loops.
280
293
- Use `else` to execute a block of code when an `if` condition is *not* true.
281
294
- Use `elif` to specify additional tests.
295
+
- Conditions are tested once, in order.
282
296
- Use `and` and `or` to check against multiple value statements.
0 commit comments