---
# System prepended metadata

title: Python Challenge Day 11 - Twice as old

---

---
title: 'Python Challenge Day 11 - Twice as old'
disqus: hackmd
---


## Python Challenge Day 11 - Twice as old
### DESCRIPTION

Your function takes two arguments:

current father's age (years)
current age of his son (years)
Сalculate how many years ago the father was twice as old as his son (or in how many years he will be twice as old). The answer is always greater or equal to 0, no matter if it was in the past or it is in the future.


程式邏輯
---
目的：計算多少年前父親的年齡是他兒子的兩倍（或多少年後他將是兒子的兩倍）
函數有兩個參數：當前父親的年齡（歲）、他兒子現在的年齡（歲）
注意：答案總是大於或等於 0

Sample Tests
---
``` python
import codewars_test as test
from solution import twice_as_old

@test.describe("Fixed Tests")
def fixed_tests():
    @test.it('Basic Test Cases')
    def basic_test_cases():
        test.assert_equals(twice_as_old(36,7) , 22)
        test.assert_equals(twice_as_old(55,30) , 5)
        test.assert_equals(twice_as_old(42,21) , 0)
        test.assert_equals(twice_as_old(22,1) , 20)
        test.assert_equals(twice_as_old(29,0) , 29)
```
Solution
---
### Practice 1:
``` python
def twice_as_old(dad_years_old, son_years_old):
    if dad_years_old < 0 and son_years_old < 0:
        return False
    else: 
        return abs(son_years_old*2 - dad_years_old) 
```

* 使用 abs() 函數回傳數字的絕對值
當父親22歲，兒子1歲，1*2-22= -20，所以要使用abs才會是答案22

### Practice 2:
``` python
def twice_as_old(dad_years_old, son_years_old):
    return abs(dad_years_old - 2*son_years_old)
```
### Practice 3:
``` python
def twice_as_old(dad_years_old, son_years_old):
    twice_old = son_years_old * 2
    years_ago = dad_years_old - twice_old
    return abs(years_ago)
```
## 參考資源

:::info
* [Kata](https://www.codewars.com/kata/5b853229cfde412a470000d0)
:::


