Skip to content

Commit fac816e

Browse files
committed
exercise 3 complete
1 parent d10f140 commit fac816e

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

‎Sprint 5 Prep Exercises/exercise_2.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ def double(number):
55

66
# Read the above code and write down what the bug is. How would you fix it?
77

8-
# Potential bug: code says double and it is multiplying by 3
9-
# We can't be sure this is a bug without knowing more information about intent of the developer or requirement of the user.
8+
# Potential bug: Code says double and it is multiplying by 3
109

11-
# Assuming a bug; it could be fixed by changing the name of the function to triple, or the integer to 2.
10+
# It could be fixed by changing the name of the function to triple, or the integer to 2.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def open_account(balances: dict[str, int], name: str, amount: int) -> None:
2+
balances[name] = amount
3+
4+
def sum_balances(accounts: dict[str, int]) -> int:
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: int) -> str:
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", 913)
25+
open_account(balances, "Olya", 713)
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}")

0 commit comments

Comments
 (0)