Skip to content

Commit e5e0319

Browse files
authored
Merge pull request #46 from quist00/fstringsForConditionals
Refactor if and else statements to use f-strings Fixes #14
2 parents bffe2b1 + 3ac1e47 commit e5e0319

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

episodes/conditionals.md

+29-29
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ exercises: 15
66

77
::::::::::::::::::::::::::::::::::::::: objectives
88

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.
1010
- Trace the execution of unnested conditionals and conditionals inside loops.
1111

1212
::::::::::::::::::::::::::::::::::::::::::::::::::
@@ -26,11 +26,11 @@ An `if` statement is a *conditional* statement that controls whether a block of
2626
```python
2727
checkouts = 11
2828
if checkouts > 10.0:
29-
print(checkouts, 'is over the checkout limit.')
29+
print(f'{checkouts}, is over the checkout limit.')
3030

3131
checkouts = 8
3232
if checkouts > 10.0:
33-
print(checkouts, 'is over the checkout limit.')
33+
print(f'{checkouts}, is over the checkout limit.')
3434
```
3535

3636
```output
@@ -45,7 +45,7 @@ There is not much of a point using a conditional when we know the value (as abov
4545
checkouts = [3, 5, 12, 22, 0]
4646
for checkout in checkouts:
4747
if checkout > 10.0:
48-
print(checkout, 'is over the checkout limit.')
48+
print(f'{checkout}, is over the checkout limit.')
4949
```
5050

5151
```output
@@ -61,9 +61,9 @@ And `else` statement can be used following `if` to allow us to specify an altern
6161
checkouts = [3, 5, 12, 22, 0]
6262
for checkout in checkouts:
6363
if checkout > 10.0:
64-
print('Warning:', checkout, 'is over the checkout limit.')
64+
print(f'Warning: {checkout} is over the checkout limit.')
6565
else:
66-
print(checkout, 'is under the limit.')
66+
print(f'{checkout} is under the limit.')
6767
```
6868

6969
```output
@@ -82,11 +82,11 @@ You can use `elif` (short for "else if") to provide several alternative choices,
8282
checkouts = [3, 5, 10, 22, 0]
8383
for checkout in checkouts:
8484
if checkout > 10.0:
85-
print('Warning:', checkout, 'is over the checkout limit.')
85+
print(f'Warning: {checkout} is over the checkout limit.')
8686
elif checkout == 10:
87-
print(checkout, 'is at the exact checkout limit.')
87+
print(f'{checkout} is at the exact checkout limit.')
8888
else:
89-
print(checkout, 'is under the limit.')
89+
print(f'{checkout} is under the limit.')
9090
```
9191

9292
```output
@@ -133,14 +133,14 @@ You can use conditionals inside of a loop to adjust values as the loop iterates.
133133
```python
134134
checkouts = 15.0
135135
for i in range(5): # execute the loop 5 times
136-
print(i, ':', checkouts)
136+
print(f'{i} : {checkouts}')
137137
if checkouts >= 30.0:
138138
print('too many checkouts')
139139
checkouts = checkouts - 5.0
140140
else:
141141
print('too few checkouts')
142142
checkouts = checkouts + 10.0
143-
print('final checkouts:', checkouts)
143+
print(f'final checkouts: {checkouts}')
144144
```
145145

146146
```output
@@ -172,13 +172,13 @@ for user in user_type:
172172
print(user)
173173
for checkout in checkouts:
174174
if checkout > 100 and user == 'faculty':
175-
print(checkout, "Over the faculty checkout limit.")
175+
print(f"{checkout} Over the faculty checkout limit.")
176176
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.")
178178
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.")
180180
else:
181-
print(checkout, "Under the checkout limit.")
181+
print(f"{checkout} Under the checkout limit.")
182182
print()
183183

184184
```
@@ -207,16 +207,16 @@ undergraduate
207207

208208
What does this program print?
209209

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)
210217
```
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-
```
218218

219-
{: python}
219+
220220

221221
::::::::::::::: solution
222222

@@ -232,7 +232,7 @@ print(pressure)
232232

233233
::::::::::::::::::::::::::::::::::::::: challenge
234234

235-
## Trimming Values
235+
## Acummulating Results Conditionally
236236

237237
Fill in the blanks so that this program creates a new list
238238
containing zeroes where the original list's values were negative
@@ -274,7 +274,7 @@ print(result)
274274

275275
::::::::::::::::::::::::::::::::::::::: challenge
276276

277-
## Processing Small Files
277+
## Processing Files Based on Record Length
278278

279279
Modify this program so that it only processes files with fewer than 50 records.
280280

@@ -284,7 +284,7 @@ import pandas
284284
for filename in glob.glob('data/*.csv'):
285285
contents = pandas.read_csv(filename)
286286
____:
287-
print(filename, len(contents))
287+
print(f'{filename} : {len(contents)}')
288288
```
289289

290290
::::::::::::::: solution
@@ -296,8 +296,8 @@ import glob
296296
import pandas
297297
for filename in glob.glob('data/*.csv'):
298298
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)}')
301301
```
302302

303303
:::::::::::::::::::::::::
@@ -306,7 +306,7 @@ for filename in glob.glob('data/*.csv'):
306306

307307
::::::::::::::::::::::::::::::::::::::: challenge
308308

309-
## Initializing
309+
## Range Finding
310310

311311
Modify this program so that it finds the largest and smallest values in the list
312312
no matter what the range of values originally is.

0 commit comments

Comments
 (0)