|
| 1 | +# exercise 5.3.6 from unit 5 |
| 2 | + |
| 3 | +''' |
| 4 | +Write a function called filter_teens defined as follows: |
| 5 | +
|
| 6 | +def filter_teens(a, b, c): |
| 7 | +The function receives three values representing ages: a, b, c and returns their sum. |
| 8 | +
|
| 9 | +Guidelines |
| 10 | +If the function is called with no parameters, the default value of each age is 13. |
| 11 | +If one of the values represents the age of teenagers between 13 and 19 (including them) |
| 12 | +but excluding the ages 15 and 16 - adjust its value using the function described below, |
| 13 | +so that it is calculated as 0. |
| 14 | +
|
| 15 | +Write a helper function fix_age defined as follows: |
| 16 | +def fix_age(age): |
| 17 | +The function receives a number that represents an age and returns it corrected |
| 18 | +according to the rules above. Call the fix_age function from within the filter_teens |
| 19 | +function so that you don't repeat the age correction code three times. |
| 20 | +
|
| 21 | +Running examples of the filter_teens function |
| 22 | +
|
| 23 | +>>> filter_teens() |
| 24 | +0 |
| 25 | +>>> filter_teens(1, 2, 3) |
| 26 | +6 |
| 27 | +>>> filter_teens(2, 13, 1) |
| 28 | +3 |
| 29 | +>>> filter_teens(2, 1, 15) |
| 30 | +18 |
| 31 | +''' |
| 32 | +def fix_age(age): |
| 33 | + if age >= 13 and age <= 19 and age not in (15, 16): |
| 34 | + return 0 |
| 35 | + return age |
| 36 | + |
| 37 | + |
| 38 | +def filter_teens(a=13, b=13, c=13): |
| 39 | + a = fix_age(a) |
| 40 | + b = fix_age(b) |
| 41 | + c = fix_age(c) |
| 42 | + return a + b + c |
| 43 | + |
| 44 | +print(filter_teens()) # 0 |
| 45 | +print(filter_teens(1, 2, 3)) # 6 |
| 46 | +print(filter_teens(2, 1, 15)) # 18 |
| 47 | + |
| 48 | + |
0 commit comments