Skip to content

Commit 82dae40

Browse files
committed
feat: Add the birthday logic to the method.
1 parent 8663043 commit 82dae40

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

prep-exercises/methods.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import datetime as dt # we import the datetime module
2+
3+
# this is similar to new Date() object in js, but only getting the date not hours etc.
4+
5+
class Person:
6+
def __init__(self, name: str, preferred_os: str, dob: dt.date):
7+
self.name = name
8+
self.preferred_operating_system = preferred_os
9+
self.date_of_birth = dob
10+
11+
def is_adult(self) -> bool:
12+
today = dt.date.today()
13+
age = today.year - self.date_of_birth.year
14+
if (today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day):
15+
# if the birthday date did not happened yet ad -1 else skip it, we use full age
16+
age -= 1
17+
return age >= 18
18+
19+
imran_dob = dt.date(1986, 12, 24)
20+
imran = Person("Imran", "Ubuntu", imran_dob)
21+
22+
print(f"{imran.name} is adult: {imran.is_adult()}")

0 commit comments

Comments
 (0)