Error in-Budget App

Hello,

I am getting the following error when I am trying to run the code for budget app. Some of the earlier error messages were clearm, but I am not sure what is the issue here-
.F…

FAIL: test_create_spend_chart (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-budget-app-3/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[32 chars] \n 90| \n 80| \n 70| o \n[270 chars] t ’ != 'Perc[32 chars] \n 90| \n 80| \n 70|[342 chars] t ’
Diff is 1199 characters long. Set self.maxDiff to None to see it. : Expected different chart representation. Check that all spacing is exact.

Following is the code I am running -
def truncate(n):
multiplier = 10
return int(n*multiplier) / multiplier

def getTotals(categories):
total = 0
breakdown =
for category in categories:
total += category.get_withdrawals()
breakdown.append(category.get_withdrawals())
rounded = list(map(lambda x: truncate(x/total), breakdown))
return rounded

def create_spend_chart(categories):
res = “Percentage spent by category\n”
i = 100
totals = getTotals(categories)
while i >= 0:
cat_spaces = " "
for total in totals:
if total * 100 >= i:
cat_spaces += “o "
else:
cat_spaces += " "
res+= str(i).rjust(3) + “|” + cat_spaces + (”\n")
i-=10

dashes = “-” + “—”*len(categories)
names =
x_axis = “”
for category in categories:
names.append(category.name)

maxi = max(names, key=len)

for x in range(len(maxi)):
nameStr = ’ ’
for name in names:
if x >= len(name):
nameStr += " "
else:
nameStr += name + " "

  if(x != len(maxi) -1 ):
    nameStr += '\n'

  
  x_axis += nameStr

res+= dashes.rjust(len(dashes)+4) + “\n” + x_axis
return res

class Category:

def init(self, name):
self.name = name
self.ledger = list()

def str(self):
title = f"{self.name:*^30}\n"
items = “”
total = 0
for i in range(len(self.ledger)):
items += f"{self.ledger[i][‘description’][0:23]:23}" + f"{self.ledger[i][‘amount’]:>7.2f}" + ‘\n’
total += self.ledger[i][‘amount’]

    output = title + items + "Total: " + str(total)
    return output 

def deposit (self, amount, description = “”):
self.ledger.append({“amount”:amount, “description”:description})

def withdraw (self, amount, description=""):
if(self.check_funds(amount)):
self.ledger.append({“amount”: -amount, “description”: description})
return True
return False

def get_balance(self):
total_cash = 0
for item in self.ledger:
total_cash += item[“amount”]
return total_cash

def transfer(self, amount, category):
if (self.check_funds(amount)):
self.withdraw(amount,"Transfer to " + category.name)
category.deposit(amount,"Transfer from " + self.name)
return True
return False

def check_funds(self, amount):
if(self.get_balance() >= amount):
return True
return False

category method

def get_withdrawals(self):
total = 0
for item in self.ledger:
if item[“amount”] < 0:
total += item[“amount”]
return total

It would be better to link to your code if your are working on replit or so - simply because of the formatting and such making reading and navigating the code easier.

For a start, let’s split up the error:

'Perc[32 chars] \n 90| \n 80| \n 70| o \n[270 chars] t '
'Perc[32 chars] \n 90| \n 80| \n 70|[342 chars] t '

The first one is your output, the second one the expected one.
It would be nice to see created chart printed, but you can easily see that your output is significantly shorter with only 270 characters at the end instead of the expected 342.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.