I think my output is already the same with what the unit test is asking but it still returns error. please correct me if I’m wrong, thanks for reading
here is my code:
class Category:
def __init__(self, category):
self.ledger = []
self.funds = 0
self.spending = 0
self.category = category
def check_funds(self, amount):
if ((self.funds - amount) < 0):
return False
else:
return True
def deposit(self, amount, desc=None):
self.amount = amount
self.desc = desc
if (self.desc == None):
self.desc = ""
self.funds += self.amount
self.ledger.append({"amount": self.amount, "description": self.desc})
def withdraw(self, amount, desc=None):
self.amount = amount
self.desc = desc
if (self.desc == None):
self.desc = ""
self.amount -= amount * 2
if (self.check_funds(amount) == False):
return False
else:
self.funds += self.amount
self.spending += self.amount
self.ledger.append({"amount": self.amount, "description": self.desc})
return True
def get_balance(self):
return self.funds
def transfer(self, amount, other):
self.amount = amount
self.desc = "Transfer to " + other.category
other.desc = "Transfer from " + self.category
other.amount = self.amount
self.amount -= amount * 2
if (self.check_funds(amount) == False):
return False
else:
self.funds += self.amount
self.spending += self.amount
other.funds += other.amount
self.ledger.append({"amount": self.amount, "description": self.desc})
other.ledger.append({"amount": other.amount, "description": other.desc})
return True
def __str__(self):
title = '{:*^30}'.format(self.category)
contents = []
for x in self.ledger:
content = []
for values in x.values():
content.append(values)
amount = content[0]
desc = content[1]
text = '{:<23.23}{:>7.2f}'.format(desc, amount) #23.23 means width is 23 digits and 23 digits max and cannot surpass it
contents.append(text)
content_str = "\n".join(contents)
output = title + '\n' + content_str + '\n' + "Total: " + str(self.funds)
return (output)
def create_spend_chart(categories):
title = "Percentage spent by category"
category_names = []
category_spendings = []
category_percentage = []
chart_percentage = ['100|', ' 90|', ' 80|', ' 70|', ' 60|', ' 50|', " 40|", " 30|", " 20|", " 10|", " 0|"]
total_spendings = 0
#formatting for numbers
for x in categories:
name = x.category
spent = round(x.spending,-1)
total_spendings += spent
category_names.append(name)
category_spendings.append(spent)
for x in category_spendings:
category_percentage.append(round(x/total_spendings * 100))
for x in category_percentage:
index = 0
for y in chart_percentage:
number = ""
for nums in y:
if nums.isdigit():
number += nums
if float(number) <= x:
chart_percentage[index] += " o "
else:
chart_percentage[index] += " "
index += 1
percentage_output = "\n".join(chart_percentage)
#formatting for strips
strip = " "
for x in category_names:
strip += "---"
strip += "-"
#formatting for names
length = 0
for x in category_names:
if length < len(x):
length = len(x)
i= 0
names_list = []
while i <= length:
lines = " "
for x in category_names:
if i < len(x):
lines += " " + x[i] + " "
else:
lines += " "
i += 1
names_list.append(lines)
names_output = "\n".join(names_list)
output = title + "\n" + percentage_output + "\n" + strip + "\n" + names_output
return(output)
and here is the error code:
FAIL: test_create_spend_chart (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-budget-app/test_module.py", line 140, in test_create_spend_chart
self.assertEqual(
AssertionError: 'Perc[34 chars] \n 90| \n 80| \n 70| o[331 chars] ' != 'Perc[34 chars] \n 90| \n 80| \n 70| [340 chars] t '
Percentage spent by category
- 100|
+ 100|
? +
- 90|
+ 90|
? +
- 80|
+ 80|
? +
- 70| o
+ 70| o
? +
- 60| o
+ 60| o
? +
- 50| o
+ 50| o
? +
- 40| o
+ 40| o
? +
- 30| o
+ 30| o
? +
- 20| o o
+ 20| o o
? +
- 10| o o
+ 10| o o
? +
- 0| o o o
+ 0| o o o
? +
----------
- B F E
+ B F E
? +
- u o n
+ u o n
? +
- s o t
+ s o t
? +
- i d e
+ i d e
? +
- n r
+ n r
? +
- e t
+ e t
? +
- s a
+ s a
? +
- s i
+ s i
? +
- n
+ n
? +
- m
+ m
? +
- e
+ e
? +
- n
+ n
? +
- t
? ^
+ t ? ^
- : Expected different chart representation. Check that all spacing is exact.
----------------------------------------------------------------------
Ran 11 tests in 0.023s
FAILED (failures=1)