Given 3 int values, a b c, return their sum. However, if any of the values is a teen -- in the range 13..19 inclusive -- then that value counts as 0, except 15 and 16 do not count as a teens. Write a separate helper "def fix_teen(n):"that takes in an int value and returns that value fixed for the teen rule. In this way, you avoid repeating the teen code 3 times (i.e. "decomposition"). Define the helper below and at the same indent level as the main no_teen_sum().
时间: 2023-12-19 10:04:33 浏览: 240
Clustering-LEACH-EAMMH-SEP-TEEN.zip_EAMMH_SEP_SEP-TEEN_leach in
以下是实现该功能的Python代码:
```python
def no_teen_sum(a, b, c):
return fix_teen(a) + fix_teen(b) + fix_teen(c)
def fix_teen(n):
if (n >= 13 and n < 15) or (n > 16 and n <= 19):
return 0
else:
return n
```
通过定义 `fix_teen(n)` 函数来处理范围在 13 到 19 之间的整数。在 `no_teen_sum(a, b, c)` 函数中,分别调用 `fix_teen(n)` 函数来处理参数 a、b、c,最终将处理后的结果相加并返回。
阅读全文