Skip to content

Commit 0ab20fb

Browse files
committed
Add methods exercise
1 parent 3b72b37 commit 0ab20fb

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+
from datetime import date
2+
3+
class Person:
4+
def __init__(self, name: str, DoB: date, preferred_operating_system: str):
5+
self.name = name
6+
self.DoB = DoB
7+
self.preferred_operating_system = preferred_operating_system
8+
9+
def is_adult(self) -> bool:
10+
current_date = date.today()
11+
age = current_date.year - self.DoB.year - (
12+
(current_date.month, current_date.day) < (self.DoB.month, self.DoB.day)
13+
)
14+
return age >= 18
15+
16+
17+
18+
eliza = Person("Eliza", date(2010, 5, 15), "Arch Linux")
19+
print(f"{eliza.name} is adult: {eliza.is_adult()}")
20+
21+
sara = Person("Sara", date(1995, 12, 20), "macOS")
22+
print(f"{sara.name} is adult: {sara.is_adult()}")

0 commit comments

Comments
 (0)