Skip to content

Commit

Permalink
Adjusted if-elif-else examples and hints. (#3764)
Browse files Browse the repository at this point in the history
  • Loading branch information
BethanyG authored Aug 29, 2024
1 parent 57f11c2 commit e4bb420
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 27 deletions.
51 changes: 39 additions & 12 deletions concepts/conditionals/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Python 3.10 introduces a variant case-switch statement called `pattern matching`
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].



```python
x = 10
y = 5
Expand Down Expand Up @@ -61,13 +60,15 @@ else:

>>> def classic_fizzbuzz(number):
if number % 3 == 0 and number % 5 == 0:
return 'FizzBuzz!'
say = 'FizzBuzz!'
elif number % 5 == 0:
return 'Buzz!'
say = 'Buzz!'
elif number % 3 == 0:
return 'Fizz!'
say = 'Fizz!'
else:
return str(number)
say = str(number)

return say

>>> classic_fizzbuzz(15)
'FizzBuzz!'
Expand All @@ -76,19 +77,44 @@ else:
'13'
```

As an alternative, the example above can be re-written to only use `if` statements with `returns`.
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.


```python
>>> def classic_fizzbuzz(number):
if number % 3 == 0 and number % 5 == 0:
return 'FizzBuzz!'
if number % 5 == 0:
return 'Buzz!'
if number % 3 == 0:
return 'Fizz!'

return str(number)

>>> classic_fizzbuzz(15)
'FizzBuzz!'

>>> classic_fizzbuzz(13)
'13'
```


Conditionals can also be nested.

```python
>>> def driving_status(driver_age, test_score):
if test_score >= 80:
if 18 > driver_age >= 16:
return "Student driver, needs supervision."
status = "Student driver, needs supervision."
elif driver_age == 18:
return "Permitted driver, on probation."
satus = "Permitted driver, on probation."
elif driver_age > 18:
return "Fully licensed driver."
status = "Fully licensed driver."
else:
return "Unlicensed!"
status = "Unlicensed!"

return status


>>> driving_status(63, 78)
Expand Down Expand Up @@ -151,8 +177,9 @@ This is Truthy.
Nope. It's Falsey.
```

[if statement]: https://docs.python.org/3/reference/compound_stmts.html#the-if-statement
[control flow tools]: https://docs.python.org/3/tutorial/controlflow.html#more-control-flow-tools
[truth value testing]: https://docs.python.org/3/library/stdtypes.html#truth-value-testing
[boolean operations]: https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not
[comparisons]: https://docs.python.org/3/library/stdtypes.html#comparisons
[control flow tools]: https://docs.python.org/3/tutorial/controlflow.html#more-control-flow-tools
[if statement]: https://docs.python.org/3/reference/compound_stmts.html#the-if-statement
[mutually-exclusive]: https://stackoverflow.com/a/22783232
[truth value testing]: https://docs.python.org/3/library/stdtypes.html#truth-value-testing
13 changes: 6 additions & 7 deletions concepts/conditionals/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ z = 20

# The elif statement allows for the checking of more conditions.
if x > y > z:

print("x is greater than y and z")
elif y > x > z:

print("y is greater than x and z")
else:
print("z is greater than x and y")
Expand All @@ -59,16 +57,17 @@ else:
[Boolean operations][boolean operations] and [comparisons][comparisons] can be combined with conditionals for more complex testing:

```python

>>> def classic_fizzbuzz(number):
if number % 3 == 0 and number % 5 == 0:
return 'FizzBuzz!'
say = 'FizzBuzz!'
elif number % 5 == 0:
return 'Buzz!'
say = 'Buzz!'
elif number % 3 == 0:
return 'Fizz!'
say = 'Fizz!'
else:
return str(number)
say = str(number)

return say

>>> classic_fizzbuzz(15)
'FizzBuzz!'
Expand Down
6 changes: 4 additions & 2 deletions exercises/concept/meltdown-mitigation/.docs/hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@

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

## 3. Fail Safe Mechanism

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


[boolean operations]: https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not
Expand Down
11 changes: 6 additions & 5 deletions exercises/concept/meltdown-mitigation/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,17 @@ else:
[Boolean operations][boolean operations] and [comparisons][comparisons] can be combined with conditionals for more complex testing:

```python

>>> def classic_fizzbuzz(number):
if number % 3 == 0 and number % 5 == 0:
return 'FizzBuzz!'
say = 'FizzBuzz!'
elif number % 5 == 0:
return 'Buzz!'
say = 'Buzz!'
elif number % 3 == 0:
return 'Fizz!'
say = 'Fizz!'
else:
return str(number)
say = str(number)

return say

>>> classic_fizzbuzz(15)
'FizzBuzz!'
Expand Down
1 change: 0 additions & 1 deletion exercises/concept/meltdown-mitigation/.meta/exemplar.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def reactor_efficiency(voltage, current, theoretical_max_power):

generated_power = voltage * current
percentage_range = (generated_power / theoretical_max_power) * 100
efficiency_level = 'unknown'

if 80 <= percentage_range <= 100:
efficiency_level = 'green'
Expand Down

0 comments on commit e4bb420

Please sign in to comment.