Skip to content

Commit e4bb420

Browse files
authored
Adjusted if-elif-else examples and hints. (#3764)
1 parent 57f11c2 commit e4bb420

File tree

5 files changed

+55
-27
lines changed

5 files changed

+55
-27
lines changed

concepts/conditionals/about.md

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ Python 3.10 introduces a variant case-switch statement called `pattern matching`
88
Conditional statements use expressions that must resolve to `True` or `False` -- either by returning a `bool` directly, or by evaluating ["truthy" or "falsy"][truth value testing].
99

1010

11-
1211
```python
1312
x = 10
1413
y = 5
@@ -61,13 +60,15 @@ else:
6160

6261
>>> def classic_fizzbuzz(number):
6362
if number % 3 == 0 and number % 5 == 0:
64-
return 'FizzBuzz!'
63+
say = 'FizzBuzz!'
6564
elif number % 5 == 0:
66-
return 'Buzz!'
65+
say = 'Buzz!'
6766
elif number % 3 == 0:
68-
return 'Fizz!'
67+
say = 'Fizz!'
6968
else:
70-
return str(number)
69+
say = str(number)
70+
71+
return say
7172

7273
>>> classic_fizzbuzz(15)
7374
'FizzBuzz!'
@@ -76,19 +77,44 @@ else:
7677
'13'
7778
```
7879

80+
As an alternative, the example above can be re-written to only use `if` statements with `returns`.
81+
However, re-writing in this way might obscure that the conditions are intended to be [_mutually exclusive_][mutually-exclusive] and could lead to future bugs or maintenance issues.
82+
83+
84+
```python
85+
>>> def classic_fizzbuzz(number):
86+
if number % 3 == 0 and number % 5 == 0:
87+
return 'FizzBuzz!'
88+
if number % 5 == 0:
89+
return 'Buzz!'
90+
if number % 3 == 0:
91+
return 'Fizz!'
92+
93+
return str(number)
94+
95+
>>> classic_fizzbuzz(15)
96+
'FizzBuzz!'
97+
98+
>>> classic_fizzbuzz(13)
99+
'13'
100+
```
101+
102+
79103
Conditionals can also be nested.
80104

81105
```python
82106
>>> def driving_status(driver_age, test_score):
83107
if test_score >= 80:
84108
if 18 > driver_age >= 16:
85-
return "Student driver, needs supervision."
109+
status = "Student driver, needs supervision."
86110
elif driver_age == 18:
87-
return "Permitted driver, on probation."
111+
satus = "Permitted driver, on probation."
88112
elif driver_age > 18:
89-
return "Fully licensed driver."
113+
status = "Fully licensed driver."
90114
else:
91-
return "Unlicensed!"
115+
status = "Unlicensed!"
116+
117+
return status
92118

93119

94120
>>> driving_status(63, 78)
@@ -151,8 +177,9 @@ This is Truthy.
151177
Nope. It's Falsey.
152178
```
153179

154-
[if statement]: https://docs.python.org/3/reference/compound_stmts.html#the-if-statement
155-
[control flow tools]: https://docs.python.org/3/tutorial/controlflow.html#more-control-flow-tools
156-
[truth value testing]: https://docs.python.org/3/library/stdtypes.html#truth-value-testing
157180
[boolean operations]: https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not
158181
[comparisons]: https://docs.python.org/3/library/stdtypes.html#comparisons
182+
[control flow tools]: https://docs.python.org/3/tutorial/controlflow.html#more-control-flow-tools
183+
[if statement]: https://docs.python.org/3/reference/compound_stmts.html#the-if-statement
184+
[mutually-exclusive]: https://stackoverflow.com/a/22783232
185+
[truth value testing]: https://docs.python.org/3/library/stdtypes.html#truth-value-testing

concepts/conditionals/introduction.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@ z = 20
4545

4646
# The elif statement allows for the checking of more conditions.
4747
if x > y > z:
48-
4948
print("x is greater than y and z")
5049
elif y > x > z:
51-
5250
print("y is greater than x and z")
5351
else:
5452
print("z is greater than x and y")
@@ -59,16 +57,17 @@ else:
5957
[Boolean operations][boolean operations] and [comparisons][comparisons] can be combined with conditionals for more complex testing:
6058

6159
```python
62-
6360
>>> def classic_fizzbuzz(number):
6461
if number % 3 == 0 and number % 5 == 0:
65-
return 'FizzBuzz!'
62+
say = 'FizzBuzz!'
6663
elif number % 5 == 0:
67-
return 'Buzz!'
64+
say = 'Buzz!'
6865
elif number % 3 == 0:
69-
return 'Fizz!'
66+
say = 'Fizz!'
7067
else:
71-
return str(number)
68+
say = str(number)
69+
70+
return say
7271

7372
>>> classic_fizzbuzz(15)
7473
'FizzBuzz!'

exercises/concept/meltdown-mitigation/.docs/hints.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030

3131
- Comparison operators can be combined and used with conditionals.
3232
- Any number of `elif` statements can be used as decision "branches".
33-
- Each "branch" can have a separate `return`
33+
- Each "branch" can have a separate `return`, although it might be considered "bad form" by linting tools.
34+
- If the linter complains, consider assigning the output of a branch to a common variable, and then `return`ing that variable.
3435

3536
## 3. Fail Safe Mechanism
3637

3738
- Comparison operators can be combined and used with conditionals.
3839
- Any number of `elif` statements can be used as decision "branches".
39-
- Each "branch" can have a separate `return`
40+
- Each "branch" can have a separate `return`, although it might be considered "bad form" by linting tools.
41+
- If the linter complains, consider assigning the output of a branch to a common variable, and then `return`ing that variable.
4042

4143

4244
[boolean operations]: https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not

exercises/concept/meltdown-mitigation/.docs/introduction.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,17 @@ else:
5656
[Boolean operations][boolean operations] and [comparisons][comparisons] can be combined with conditionals for more complex testing:
5757

5858
```python
59-
6059
>>> def classic_fizzbuzz(number):
6160
if number % 3 == 0 and number % 5 == 0:
62-
return 'FizzBuzz!'
61+
say = 'FizzBuzz!'
6362
elif number % 5 == 0:
64-
return 'Buzz!'
63+
say = 'Buzz!'
6564
elif number % 3 == 0:
66-
return 'Fizz!'
65+
say = 'Fizz!'
6766
else:
68-
return str(number)
67+
say = str(number)
68+
69+
return say
6970

7071
>>> classic_fizzbuzz(15)
7172
'FizzBuzz!'

exercises/concept/meltdown-mitigation/.meta/exemplar.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ def reactor_efficiency(voltage, current, theoretical_max_power):
4545

4646
generated_power = voltage * current
4747
percentage_range = (generated_power / theoretical_max_power) * 100
48-
efficiency_level = 'unknown'
4948

5049
if 80 <= percentage_range <= 100:
5150
efficiency_level = 'green'

0 commit comments

Comments
 (0)