I am working on the Budget App project for Python Certification and have written all the required code. But, I am not able to pass two test cases, one bring related to rounding off the percentage, and another failure is related to alignment when we print the ledger which for some reason appears for only that particular test case. My code is as follows:
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
def deposit(self, amount, des = ""):
self.ledger.append({"amount": amount, "description": des})
def withdraw(self, amount, des = ""):
if self.check_funds(amount):
self.ledger.append({"amount": -amount, "description": des})
return True
else:
return False
def get_balance(self):
bal = 0
for i in self.ledger:
bal += i["amount"]
return bal
def transfer(self, amount, cat):
if self.check_funds(amount):
self.ledger.append({"amount": -amount, "description": "Transfer to " + cat.name})
cat.ledger.append({"amount": amount, "description": "Transfer from " + self.name})
return True
else:
return False
def check_funds(self, amount):
if self.get_balance() < amount:
return False
else:
return True
def __str__(self):
total = 0
led = self.name.center(30, "*")
led += "\n"
for i in self.ledger:
des = i["description"]
des = des[:23]
while len(des) < 23:
des += " "
led += des
amt = i["amount"]
amt = "{:.2f}".format(amt)
amt = amt[:7]
total += float(amt)
led += amt
led += "\n"
led += "Total: " + "{:.2f}".format(total)
return led
def create_spend_chart(arr):
expen = 0
mon = []
for i in arr:
catexpen = 0
for j in i.ledger:
if j["amount"] < 0:
expen += j["amount"]
catexpen += j["amount"]
mon.append(catexpen)
percent = []
for i in mon:
percent.append(round((i / expen * 100) / 10) * 10)
graph = "Percentage spent by category\n"
num = 100
while num >= 0:
graph += str(num).rjust(3) + "| "
for i in percent:
if i >= num:
graph += "o "
else:
graph += " "
graph += "\n"
num -= 10
graph += " "
num = len(arr)
graph += "-" * (num * 3 +1)
graph += "\n"
graph += " "
high = 0
for i in arr:
if len(i.name) > high:
high = len(i.name)
for i in arr:
while len(i.name) != high:
i.name += " "
for i in range(high):
for j in arr:
graph += j.name[i] + " "
graph += "\n" + " "
return graph
I get the following output when I run it in Replit:
973.96
*************Food*************
initial deposit 1000.00
groceries -10.15
restaurant and more foo-15.89
Transfer to Clothing -50.00
Total: 923.96
***********Clothing***********
Transfer from Food 50.00
-25.55
Total: 24.45
Percentage spent by category
100|
90|
80|
70| o
60| o
50| o
40| o
30| o
20| o o
10| o o o
0| o o o
----------
F C A
o l u
o o t
d t o
h
i
n
g
.F...F.....
======================================================================
FAIL: test_create_spend_chart (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-budget-app/test_module.py", line 94, in test_create_spend_chart
self.assertEqual(actual, expected, 'Expected different chart representation. Check that all spacing is exact.')
AssertionError: 'Perc[170 chars] 10| o o o \n 0| o o o \n ----------[211 chars] ' != 'Perc[170 chars] 10| o o \n 0| o o o \n ----------[204 chars] t '
Percentage spent by category
100|
90|
80|
70| o
60| o
50| o
40| o
30| o
20| o o
- 10| o o o
? ---
+ 10| o o
? +++
0| o o o
----------
B F E
u o n
s o t
i d e
n r
e t
s a
s i
n
m
e
n
- t
? -
+ t - : Expected different chart representation. Check that all spacing is exact.
======================================================================
FAIL: test_to_string (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-budget-app/test_module.py", line 83, in test_to_string
self.assertEqual(actual, expected, 'Expected different string representation of object.')
AssertionError: '****[46 chars] 900.00\nmilk, cereal, eggs, bac-45.67\nTr[38 chars]4.33' != '****[46 chars] 900.00\nmilk, cereal, eggs, bac -45.67\n[41 chars]4.33'
*************Food*************
- deposit 900.00
+ deposit 900.00
? +
- milk, cereal, eggs, bac-45.67
+ milk, cereal, eggs, bac -45.67
? +
- Transfer to Entertainme-20.00
+ Transfer to Entertainme -20.00
? +
Total: 834.33 : Expected different string representation of object.
----------------------------------------------------------------------
Ran 11 tests in 0.021s
FAILED (failures=2)
I am not able to figure out why am I getting an alignment problem with only one test case and also I can’t think of anything about how to resolve the rounding off the percentage problem. It would be much appreciated if you helped.