Hi guys,
please help me to fix this error in create_spend_chart function
FAIL: test_create_spend_chart (test_module.UnitTests)
Traceback (most recent call last):
File “/home/runner/xomeoi7ul6r/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[34 chars] \n 90| \n 80| \n 70| o[315 chars] t’ != 'Perc[34 chars] \n 90| \n 80| \n 70| [340 chars] t ’
Diff is 1256 characters long. Set self.maxDiff to None to see it. : Expected different chart representation. Check that all spacing is exact.
Ran 11 tests in 0.012s
FAILED (failures=1)
class Category:
def __init__(self, category):
self.category = category
self.ledger = []
self.balance = 0
def deposit(self, amount, description=""):
self.ledger.append(dict(amount=amount, description=description))
self.balance += amount
def withdraw(self, amount, description=""):
funds = self.check_funds(amount)
if funds:
self.ledger.append(dict(amount=-amount, description=description))
self.balance -= amount
return True
return False
def get_balance(self):
return self.balance
def transfer(self, amount, category):
funds = self.check_funds(amount)
if funds:
self.withdraw(amount, "Transfer to " + category.category)
category.deposit(amount, "Transfer from " + self.category)
return True
return False
def check_funds(self, amount):
if self.balance < amount:
return False
return True
def __str__(self):
text = "*" * ((30 - len(self.category)) // 2) + self.category
text += "*" * (30 - len(text)) + "\n"
for item in self.ledger:
text += (
item["description"][:23].ljust(23)
+ str("{:.2f}".format(item["amount"]).rjust(7))
+ "\n"
)
text += "Total: " + str(self.balance)
return text
def create_spend_chart(categories):
chart = "Percentage spent by category\n"
total = 0
cat_spents = []
for cat in categories:
spent = 0
for withdraw in cat.ledger:
if withdraw["amount"] < 0:
total += -withdraw["amount"]
spent += -withdraw["amount"]
cat_spents.append(spent)
percentage = []
for item in cat_spents:
percentage.append(round(((item / total) // 0.01), -1))
for x in range(100, -10, -10):
chart += str(x).rjust(3, " ") + "|"
for perc in percentage:
if perc >= x:
chart += " o "
else:
chart += " "
chart += "\n"
chart += " " + "-" * len(percentage) * 3 + "-\n"
m_len = max(len(cat.category) for cat in categories)
for x in range(m_len):
chart += " "
for i in categories:
if x < len(i.category):
chart += " " + i.category[x] + " "
else:
chart += " "
chart += "\n"
return chart.rstrip()
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0
.
Challenge: Budget App
Link to the challenge: