@@ -6,7 +6,7 @@ exercises: 15
6
6
7
7
::::::::::::::::::::::::::::::::::::::: objectives
8
8
9
- - Correctly write programs that use if and else statements and simple Boolean expressions (without logical operators) .
9
+ - Correctly write programs that use if and else statements using Boolean expressions.
10
10
- Trace the execution of unnested conditionals and conditionals inside loops.
11
11
12
12
::::::::::::::::::::::::::::::::::::::::::::::::::
@@ -26,11 +26,11 @@ An `if` statement is a *conditional* statement that controls whether a block of
26
26
``` python
27
27
checkouts = 11
28
28
if checkouts > 10.0 :
29
- print (checkouts, ' is over the checkout limit.' )
29
+ print (f ' { checkouts} , is over the checkout limit.' )
30
30
31
31
checkouts = 8
32
32
if checkouts > 10.0 :
33
- print (checkouts, ' is over the checkout limit.' )
33
+ print (f ' { checkouts} , is over the checkout limit.' )
34
34
```
35
35
36
36
``` output
@@ -45,7 +45,7 @@ There is not much of a point using a conditional when we know the value (as abov
45
45
checkouts = [3 , 5 , 12 , 22 , 0 ]
46
46
for checkout in checkouts:
47
47
if checkout > 10.0 :
48
- print (checkout, ' is over the checkout limit.' )
48
+ print (f ' { checkout} , is over the checkout limit.' )
49
49
```
50
50
51
51
``` output
@@ -61,9 +61,9 @@ And `else` statement can be used following `if` to allow us to specify an altern
61
61
checkouts = [3 , 5 , 12 , 22 , 0 ]
62
62
for checkout in checkouts:
63
63
if checkout > 10.0 :
64
- print (' Warning:' , checkout, ' is over the checkout limit.' )
64
+ print (f ' Warning: { checkout} is over the checkout limit.' )
65
65
else :
66
- print (checkout, ' is under the limit.' )
66
+ print (f ' { checkout} is under the limit.' )
67
67
```
68
68
69
69
``` output
@@ -82,11 +82,11 @@ You can use `elif` (short for "else if") to provide several alternative choices,
82
82
checkouts = [3 , 5 , 10 , 22 , 0 ]
83
83
for checkout in checkouts:
84
84
if checkout > 10.0 :
85
- print (' Warning:' , checkout, ' is over the checkout limit.' )
85
+ print (f ' Warning: { checkout} is over the checkout limit.' )
86
86
elif checkout == 10 :
87
- print (checkout, ' is at the exact checkout limit.' )
87
+ print (f ' { checkout} is at the exact checkout limit.' )
88
88
else :
89
- print (checkout, ' is under the limit.' )
89
+ print (f ' { checkout} is under the limit.' )
90
90
```
91
91
92
92
``` output
@@ -133,14 +133,14 @@ You can use conditionals inside of a loop to adjust values as the loop iterates.
133
133
``` python
134
134
checkouts = 15.0
135
135
for i in range (5 ): # execute the loop 5 times
136
- print (i, ' : ' , checkouts)
136
+ print (f ' { i } : { checkouts} ' )
137
137
if checkouts >= 30.0 :
138
138
print (' too many checkouts' )
139
139
checkouts = checkouts - 5.0
140
140
else :
141
141
print (' too few checkouts' )
142
142
checkouts = checkouts + 10.0
143
- print (' final checkouts:' , checkouts)
143
+ print (f ' final checkouts: { checkouts} ' )
144
144
```
145
145
146
146
``` output
@@ -172,13 +172,13 @@ for user in user_type:
172
172
print (user)
173
173
for checkout in checkouts:
174
174
if checkout > 100 and user == ' faculty' :
175
- print (checkout, " Over the faculty checkout limit." )
175
+ print (f " { checkout} Over the faculty checkout limit." )
176
176
elif checkout > 50 and user == ' graduate' :
177
- print (checkout, " Over the graduate student checkout limit." )
177
+ print (f " { checkout} Over the graduate student checkout limit." )
178
178
elif checkout > 20 and user == ' undergraduate' :
179
- print (checkout, " Over the undergraduate student checkout limit." )
179
+ print (f " { checkout} Over the undergraduate student checkout limit." )
180
180
else :
181
- print (checkout, " Under the checkout limit." )
181
+ print (f " { checkout} Under the checkout limit." )
182
182
print ()
183
183
184
184
```
@@ -207,16 +207,16 @@ undergraduate
207
207
208
208
What does this program print?
209
209
210
+ ``` python
211
+ velocity = 71.9
212
+ if velocity > 50.0 :
213
+ velocity = 25.0
214
+ elif velocity <= 50.0 :
215
+ velocity = 0.0
216
+ print (velocity)
210
217
```
211
- pressure = 71.9
212
- if pressure > 50.0:
213
- pressure = 25.0
214
- elif pressure <= 50.0:
215
- pressure = 0.0
216
- print(pressure)
217
- ```
218
218
219
- {: python}
219
+
220
220
221
221
::::::::::::::: solution
222
222
@@ -232,7 +232,7 @@ print(pressure)
232
232
233
233
::::::::::::::::::::::::::::::::::::::: challenge
234
234
235
- ## Trimming Values
235
+ ## Acummulating Results Conditionally
236
236
237
237
Fill in the blanks so that this program creates a new list
238
238
containing zeroes where the original list's values were negative
@@ -274,7 +274,7 @@ print(result)
274
274
275
275
::::::::::::::::::::::::::::::::::::::: challenge
276
276
277
- ## Processing Small Files
277
+ ## Processing Files Based on Record Length
278
278
279
279
Modify this program so that it only processes files with fewer than 50 records.
280
280
@@ -284,7 +284,7 @@ import pandas
284
284
for filename in glob.glob(' data/*.csv' ):
285
285
contents = pandas.read_csv(filename)
286
286
____ :
287
- print (filename, len (contents))
287
+ print (f ' { filename} : { len (contents)} ' )
288
288
```
289
289
290
290
::::::::::::::: solution
@@ -296,8 +296,8 @@ import glob
296
296
import pandas
297
297
for filename in glob.glob(' data/*.csv' ):
298
298
contents = pandas.read_csv(filename)
299
- if len (contents) < 50 :
300
- print (filename, len (contents))
299
+ if len (contents) < 85 :
300
+ print (f ' { filename} : { len (contents)} ' )
301
301
```
302
302
303
303
:::::::::::::::::::::::::
@@ -306,7 +306,7 @@ for filename in glob.glob('data/*.csv'):
306
306
307
307
::::::::::::::::::::::::::::::::::::::: challenge
308
308
309
- ## Initializing
309
+ ## Range Finding
310
310
311
311
Modify this program so that it finds the largest and smallest values in the list
312
312
no matter what the range of values originally is.
0 commit comments