Skip to content

Commit 172f9c6

Browse files
committed
adding the missing code in typeAnnotation.py file
1 parent af4fe85 commit 172f9c6

2 files changed

Lines changed: 24 additions & 13 deletions

File tree

‎sprint5-prep-exercises/generics.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from dataclasses import dataclass
2+
from typing import List
3+
4+
@dataclass(frozen=True)
5+
class Person:
6+
name: str
7+
children: List["Person"]
8+
age: int
9+
10+
fatma = Person(name="Fatma", children=[], age=10)
11+
aisha = Person(name="Aisha", children=[], age=13)
12+
13+
imran = Person(name="Imran", children=[fatma, aisha], age=50)
14+
15+
def print_family_tree(person: Person) -> None:
16+
print(person.name)
17+
for child in person.children:
18+
print(f"- {child.name} ({child.age})")
19+
20+
print_family_tree(imran)
Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,30 @@
1-
2-
def open_account(balances: dict[str, int], name: str, amount: int):
1+
from typing import Dict
2+
def open_account(balances: dict, name: str, amount: int)-> None:
33
balances[name] = amount
4-
5-
6-
def sum_balances(accounts: dict[str, int]) -> int:
4+
def sum_balances(accounts: Dict[str, int]) -> int:
75
total = 0
86
for name, pence in accounts.items():
97
print(f"{name} had balance {pence}")
108
total += pence
119
return total
1210

13-
1411
def format_pence_as_string(total_pence: int) -> str:
1512
if total_pence < 100:
1613
return f"{total_pence}p"
1714
pounds = int(total_pence / 100)
1815
pence = total_pence % 100
1916
return f"£{pounds}.{pence:02d}"
2017

21-
22-
balances: dict[str, int] = {
18+
balances = {
2319
"Sima": 700,
2420
"Linn": 545,
2521
"Georg": 831,
2622
}
2723

28-
2924
open_account(balances, "Tobi", 913)
3025
open_account(balances, "Olya", 713)
3126

3227
total_pence = sum_balances(balances)
3328
total_string = format_pence_as_string(total_pence)
3429

3530
print(f"The bank accounts total {total_string}")
36-
def is_adult(person: Person) -> bool:
37-
return person.age >= 18
38-
39-
print(is_adult(imran))

0 commit comments

Comments
 (0)