Skip to content

Commit c02301b

Browse files
committed
Type-checking
1 parent 407b010 commit c02301b

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

type-checking.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# his code contains bugs related to types. They are bugs mypy can catch.
2+
3+
# Read this code to understand what it’s trying to do.
4+
# Add type annotations to the method parameters and return types of this code.
5+
# Run the code through mypy, and fix all of the bugs that show up.
6+
# When you’re confident all of the type annotations are correct, and the bugs are fixed, run the code and check it works.
7+
8+
9+
10+
def open_account(balances, name, amount):
11+
balances[name] = amount
12+
13+
def sum_balances(accounts):
14+
total = 0
15+
for name, pence in accounts.items():
16+
print(f"{name} had balance {pence}")
17+
total += pence
18+
return total
19+
20+
def format_pence_as_string(total_pence):
21+
if total_pence < 100:
22+
return f"{total_pence}p"
23+
pounds = int(total_pence / 100)
24+
pence = total_pence % 100
25+
return f"£{pounds}.{pence:02d}"
26+
27+
balances = {
28+
"Sima": 700,
29+
"Linn": 545,
30+
"Georg": 831,
31+
}
32+
33+
# convert pounds to pence
34+
open_account(balances, "Tobi", int(9.13 * 100)) # 913 pence
35+
open_account(balances, "Olya", int(7.13 * 100)) # 713 pence
36+
37+
total_pence = sum_balances(balances)
38+
total_string = format_pence_as_string(total_pence)
39+
40+
print(f"The bank accounts total {total_string}")
41+
42+
43+
44+
45+
# When running mypy, I get the following errors:
46+
47+
# type-checking.py:24: error: Missing positional argument "amount" in call to "open_account" [call-arg]
48+
# type-checking.py:25: error: Missing positional argument "amount" in call to "open_account" [call-arg]
49+
# type-checking.py:28: error: Name "format_pence_as_str" is not defined [name-defined]
50+
# type-checking.py:34: error: Missing positional argument "amount" in call to "open_account" [call-arg]
51+
# type-checking.py:35: error: Missing positional argument "amount" in call to "open_account" [call-arg]
52+
# type-checking.py:38: error: Name "format_pence_as_str" is not defined [name-defined]
53+
54+
55+
# To fix this code, I need to add type annotations and correct the function calls as follows:
56+
57+
# wrong arguments to open_account, the function expects three arguments: balances, name, and amount.
58+
# one call passes a string "£7.13" instead of a number.
59+
# Wrong function name format_pence_as_str instead of format_pence_as_string.
60+
# To keep the program consistent (Everything in pence), convert pounds to pence when opening accounts.
61+
62+
# Here is the corrected code:
63+
64+
"""
65+
def open_account(balances, name, amount):
66+
balances[name] = amount
67+
68+
def sum_balances(accounts):
69+
total = 0
70+
for name, pence in accounts.items():
71+
print(f"{name} had balance {pence}")
72+
total += pence
73+
return total
74+
75+
def format_pence_as_string(total_pence):
76+
if total_pence < 100:
77+
return f"{total_pence}p"
78+
pounds = int(total_pence / 100)
79+
pence = total_pence % 100
80+
return f"£{pounds}.{pence:02d}"
81+
82+
balances = {
83+
"Sima": 700,
84+
"Linn": 545,
85+
"Georg": 831,
86+
}
87+
88+
# convert pounds to pence
89+
open_account(balances, "Tobi", int(9.13 * 100)) # 913 pence
90+
open_account(balances, "Olya", int(7.13 * 100)) # 713 pence
91+
92+
total_pence = sum_balances(balances)
93+
total_string = format_pence_as_string(total_pence)
94+
95+
print(f"The bank accounts total {total_string}")
96+
"""
97+

0 commit comments

Comments
 (0)