Hey everyone,
So I am done with the Budget App’s Spend Chart but I keep getting this unittest fail and its the only failed test:
FAIL: test_create_spend_chart (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-budget-app-1/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[336 chars] ' != 'Perc[34 chars] \n 90| \n 80| \n 70| [340 chars] t '
Diff is 861 characters long. Set self.maxDiff to None to see it. : Expected different chart representation. Check that all spacing is exact.
Here is the chart:
I know it has to do with something in the spacing at the end of the returned string so I tried to strip it but it didn’t work.
This is my create_spend_chart function:
def create_spend_chart(categories):
percentages = []
count = 0
# find out percentage of withdraws of all categories
total_percentage = categories[count].total_withdraws
for i in range(2):
count += 1
total_percentage += categories[count].total_withdraws
for category in categories:
percentages.append(int(round((category.total_withdraws / total_percentage) * 100)))
# set some variables
chart = []
num = 100
final_chart = ["Percentage spent by category\n"]
bottom_line = "-"
names = []
vertical_names = [" "]
categories_str = []
index = 0
int1 = 4
for category in categories:
categories_str.append(str(category.name))
# create bottom line and split names into a list of letters
for c in categories_str:
bottom_line += "---"
name = str(c).ljust(len(max(categories_str, key=len)))
names.append(list(name))
# create vertical names
for i in range(len(names[0])):
for lis in names:
vertical_names.append(lis[index] + " ")
if len(vertical_names) == int1:
vertical_names.append("\n" + " ")
int1 += 4
index += 1
names_str = "".join(vertical_names)
# create "blueprint" of the chart with decreasing percentages from 100 to 0
for i in range(11):
chart.append(f"{str(num).rjust(3)}|")
num -= 10
# add the "o's" to mark the percetage spent by each category
for ele in chart:
els = ele.split("|")
num2 = int(els[0])
for percentage in percentages:
if percentage >= num2:
els.append(" o ")
else:
els.append(" ")
els.insert(1, "|")
line = "".join(els)
final_chart.append(line + "\n")
# convert the final_chart var to a string
final_chart.append(" " + bottom_line + "\n" + names_str)
final_chart = "".join(final_chart)
return final_chart
I think the problem is somewhere in here:
# add the "o's" to mark the percetage spent by each category
for ele in chart:
els = ele.split("|")
num2 = int(els[0])
for percentage in percentages:
if percentage >= num2:
els.append(" o ")
else:
els.append(" ")
els.insert(1, "|")
line = "".join(els)
final_chart.append(line + "\n")
# convert the final_chart var to a string
final_chart.append(" " + bottom_line + "\n" + names_str)
final_chart = "".join(final_chart)
return final_chart
Again, I tried using strip, lstrip and it didn’t work.
Thank you a lot in advance!