Skip to content

Commit d7b5b7a

Browse files
committed
I just used datetime in a class to calculate the age of person class instances
1 parent 16ace89 commit d7b5b7a

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

prep exercises/bank_account.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def open_account(balances, name, amount):
2+
balances[name] = amount
3+
4+
def sum_balances(accounts):
5+
total = 0
6+
for name, pence in accounts.items():
7+
print(f"{name} had balance {pence}")
8+
total += pence
9+
return total
10+
11+
def format_pence_as_string(total_pence):
12+
if total_pence < 100:
13+
return f"{total_pence}p"
14+
pounds = int(total_pence / 100)
15+
pence = total_pence % 100
16+
return f"£{pounds}.{pence:02d}"
17+
18+
balances = {
19+
"Sima": 700,
20+
"Linn": 545,
21+
"Georg": 831,
22+
}
23+
24+
open_account(balances, "Tobi", 9.13)
25+
open_account(balances, "Olya", "£7.13")
26+
27+
total_pence = sum_balances(balances)
28+
total_string = format_pence_as_string(total_pence)
29+
30+
print(f"The bank accounts total {total_string}")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from datetime import date
2+
3+
class Person:
4+
def __init__(self, name: str, date_of_birth: str, preferred_operating_system: str, address: str):
5+
self.name = name
6+
self.date_of_birth = date.fromisoformat(date_of_birth)
7+
self.preferred_operating_system = preferred_operating_system
8+
self.address = address
9+
10+
def is_adult(self):
11+
today_date = date.today()
12+
13+
return (today_date.year - self.date_of_birth.year) >= 18
14+
15+
16+
imran = Person("Imran", "2005-12-04", "Ubunut", "Wardend Road, Birmingham")
17+
18+
print(imran.is_adult())

prep exercises/test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def double(n):
2+
return n * 3
3+
4+
num = double(21)
5+
print(num)

0 commit comments

Comments
 (0)