Skip to content

Commit 77ca2f6

Browse files
committed
Refactor Person class: replace age field with date of birth parameter
1 parent 4e2e07e commit 77ca2f6

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Sprint5/methods.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from datetime import date
2+
class Person:
3+
def __init__(self, name: str, birth_day: date):
4+
self.name = name
5+
self.birth_day = birth_day
6+
7+
def age(self)-> int: #returns age in years
8+
today = date.today()
9+
years = today.year - self.birth_day.year
10+
if(today.month, today.day) < (self.birth_day.month, self.birth_day.day):
11+
years -= 1
12+
return years
13+
14+
def is_adult(self):
15+
return self.age() >= 18
16+
17+
def __str__(self) -> str:
18+
return f"{self.name}, born {self.birth_day}, age {self.age()}"
19+
20+
p1 = Person("Sara", date(1996, 10, 28))
21+
print(p1.is_adult()) # True
22+
23+
p2 = Person("Muhib", date(2018, 6, 22))
24+
print(p2.is_adult()) # False

0 commit comments

Comments
 (0)