Skip to content

Commit f828703

Browse files
Boshra MahmoudiBoshra Mahmoudi
authored andcommitted
Improve code using Python built-in methods and floor division
1 parent b3a54f3 commit f828703

1 file changed

Lines changed: 3 additions & 5 deletions

File tree

‎sprint5/Exercise1.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@ def open_account(balances: dict[str,int], name: str, amount: int) -> None:
22
balances[name] = amount
33

44
def sum_balances(accounts: dict[str, int]) -> int:
5-
total = 0
65
for name, pence in accounts.items():
76
print(f"{name} had balance {pence}")
8-
total += pence
9-
return total
7+
return sum(accounts.values())
108

119
def format_pence_as_string(total_pence: int) -> str:
1210
if total_pence < 100:
1311
return f"{total_pence}p"
14-
pounds = int(total_pence / 100)
15-
pence = total_pence % 100
12+
pounds = total_pence // 100
13+
pence = total_pence % 100
1614
return f"£{pounds}.{pence:02d}"
1715

1816
balances = {

0 commit comments

Comments
 (0)