Skip to content

Commit b642106

Browse files
committed
task: Complete step 5 prep exercises
1 parent e40511e commit b642106

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

prep-exercises/dataclasses_ex.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# ------------------------
2+
# Q. Write a Person class using @datatype which uses a datetime.date for date of birth, rather than an int for age.
3+
4+
# Re-add the is_adult method to it.
5+
# ------------------------
6+
7+
# A.
8+
9+
import datetime as dt
10+
from dataclasses import dataclass
11+
12+
13+
@dataclass
14+
class Person:
15+
name: str
16+
birthdate: dt.date
17+
preferred_operating_system: str
18+
19+
def is_adult(self) -> bool:
20+
today = dt.date.today()
21+
age = today.year - self.birthdate.year
22+
birthday_this_year = dt.date(
23+
today.year, self.birthdate.month, self.birthdate.day)
24+
if today < birthday_this_year:
25+
age -= 1
26+
return age >= 18
27+
28+
29+
imran = Person("Imran", dt.date(2008, 3, 21), "Ubuntu")
30+
print(imran.is_adult())

0 commit comments

Comments
 (0)