Skip to content

Commit 3eeccb9

Browse files
committed
Methods exercise
1 parent 17560c8 commit 3eeccb9

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

sprint5-prep-exercises/methods.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from datetime import date
2+
3+
4+
class Person:
5+
def __init__(self, name: str, date_of_birth: date, preferred_operating_system: str) -> None:
6+
self.name = name
7+
self.date_of_birth = date_of_birth
8+
self.preferred_operating_system = preferred_operating_system
9+
10+
def is_adult(self) -> bool:
11+
today = date.today()
12+
has_had_birthday_this_year = (today.month, today.day) >= (
13+
self.date_of_birth.month,
14+
self.date_of_birth.day,
15+
)
16+
age = today.year - self.date_of_birth.year
17+
if not has_had_birthday_this_year:
18+
age -= 1
19+
return age >= 18
20+
21+
22+
imran = Person("Imran", date(2002, 6, 15), "Ubuntu")
23+
eliza = Person("Eliza", date(1990, 11, 20), "Arch Linux")
24+
25+
print(imran.name)
26+
print(imran.is_adult())
27+
print(eliza.name)
28+
print(eliza.is_adult())

0 commit comments

Comments
 (0)