Skip to content

Commit d1f8599

Browse files
committed
Completed "Type checking with mypy" exercise.
1 parent cf12c0a commit d1f8599

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def open_account(balances: dict[str, float], name: str, amount: float) -> None:
2+
balances[name] = amount
3+
4+
def sum_balances(accounts: dict[str, float]) -> float:
5+
total: float = 0
6+
7+
for name, pence in accounts.items():
8+
print(f"{name} had balance {pence}")
9+
total += pence
10+
11+
return total
12+
13+
def format_pence_as_string(total_pence: float) -> str:
14+
if total_pence < 100:
15+
return f"{total_pence}p"
16+
17+
pounds = int(total_pence / 100)
18+
pence = int(total_pence % 100)
19+
20+
return f"£{pounds}.{pence:02d}"
21+
22+
balances: dict[str, float] = {
23+
"Sima": 700,
24+
"Linn": 545,
25+
"Georg": 831,
26+
}
27+
28+
open_account(balances, "Tobi", 9.13)
29+
open_account(balances, "Olya", 7.13)
30+
31+
total_pence = sum_balances(balances)
32+
total_string = format_pence_as_string(total_pence)
33+
34+
print(f"The bank accounts total {total_string}")

0 commit comments

Comments
 (0)