Skip to content

Commit 0f1a924

Browse files
committed
types_banc_account_exer
1 parent 416bc20 commit 0f1a924

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

typesExer.py

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

0 commit comments

Comments
 (0)