----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-budget-app/test_module.py", line 102, in test_create_spend_chart
self.assertEqual(actual, expected, 'Expected different chart representation. Check that all spacing is exact.')
AssertionError: 'Perc[364 chars] m \n e \n n \n t \n' != 'Perc[364 chars] m \n e \n n \n t '
Percentage spent by category
100|
90|
80|
70| o
60| o
50| o
40| o
30| o
20| 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.
----------------------------------------------------------------------
Ran 11 tests in 0.002s
FAILED (failures=1)
Here is my error (it is the only one and it only exists for the final test), I’ve tried trimming in anyway possible, adding one space but it only changes my error output
And here is my code :
class Category:
def __init__(self, category):
self.name = category
self.ledger = []
self.balance = 0
self.total_withdraw = 0
def deposit(self, amount, description = ""):
self.ledger.append({"amount": amount, "description": description})
self.balance += amount
def withdraw(self, amount, description = ""):
if self.check_funds(amount):
self.ledger.append({"amount": -amount, "description": description})
self.balance -= amount
self.total_withdraw += amount
return True
else:
return False
def get_balance(self):
return self.balance
def transfer(self, amount, category):
if self.check_funds(amount):
self.ledger.append({"amount": -amount, "description": f"Transfer to {category.name}"})
self.balance -= amount
category.balance += amount
category.ledger.append({"amount": amount, "description": f"Transfer from {self.name}"})
return True
else:
return False
def check_funds(self, amount):
return self.balance - amount >= 0
def __str__(self):
string = '{:*^30}'.format(self.name) + "\n"
for item in self.ledger:
string += "{:<23}{:>7.2f}".format(item['description'][:23], item['amount']) + "\n"
string += f"Total: {self.balance:.2f}"
return string
def create_spend_chart(categories):
num_chart = ["100| "," 90| "," 80| "," 70| "," 60| "," 50| "," 40| "," 30| "," 20| "," 10| "," 0| "," "] #12 elements
total = 0
longest = None
results = dict()
for category in categories:
total += category.total_withdraw
if longest == None or len(category.name) > longest:
longest = len(category.name)
for category in categories:
results[category.name] = category.total_withdraw / total * 100
temp_value = results[category.name] // 10 * 10
for index, value in enumerate(num_chart):
if index == 11:
num_chart[index] += "---"
elif temp_value >= int(value[:3].lstrip()):
num_chart[index] += "o "
else:
num_chart[index] += " "*3
num_chart[-1] += "-"
w, h = len(categories), longest
matrix = [[0 for x in range(w)] for y in range(h)]
for y, row in enumerate(matrix):
for x, col in enumerate(row):
if x == 0:
if len(categories[x].name) > y:
matrix[y][x] = " "*5 +categories[x].name[y] + " "*2
else:
matrix[y][x] = " "*8
else:
if len(categories[x].name) > y:
matrix[y][x] = categories[x].name[y] + " "*2
else:
matrix[y][x] = " "*3
answer = "Percentage spent by category" + "\n"
for line in num_chart:
answer += line + "\n"
for row in matrix:
for col in row:
answer += col
answer += "\n"
return answer