# Scientific Computing with Python Projects - Budget App - can't figure spend chart assertion error

Tell us what’s happening:
I’m having a problem with the assertion for the `create_spend_chart` method.
The console is showing me the following and I can’t figure out what is wrong.

``````======================================================================
FAIL: test_create_spend_chart (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/Budget-app/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[21 chars]ory\n\n100|          \n 90|          \n 80|   [357 chars]  \n' != 'Perc[21 chars]ory\n100|          \n 90|          \n 80|     [353 chars] t  '
Percentage spent by category
-
100|
90|
80|
70|    o
60|    o
50|    o
40|    o
30|    o
20|    o  o
-  10| o  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.
``````

Here is my code so far
I created a helper method to get the expenses from the list of categories. Then I convert those values into percentages rounded to the nearest 10.

Next, I create the chart using a long for loop. I know, there are better ways to do this, but hey, this should work, right?

``````def getExpenses(categories):
total = 0
withdrawals = []
for entry in categories:
total += entry.findWithdrawn()
withdrawals.append(entry.findWithdrawn())
return withdrawals

def create_spend_chart(categories):
res = "Percentage spent by category\n"
totals = getExpenses(categories)
percentages = [int(round(el / sum(totals) * 100, -1)) for el in totals]

max_len = max([len(el.Category) for el in categories])
bar = True

for level in range(100, -10 - 10 * max_len, -10):
if level >= 0:
res += f'\n{level:>3}| '
for pct_category in percentages:
if pct_category >= level:
res += 'o  '
else:
res += '   '
if level < 0:
if bar:
res += '\n    -'
for category in categories:
res += '---'
bar = False
res += '\n'
res += '     '
ind = int((-level - 10) / 10)
for entry in categories:
if ind < len(entry.Category):
res += entry.Category[ind] + '  '
else:
res += '   '
res += '\n'
return res
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36`

Challenge: Scientific Computing with Python Projects - Budget App

I’d really appreciate any feedback.
I’ve been scratching my head today for a couple hours, and I’ve been coming back to this particular challenge, not being able to complete it. For sure there is something I’m missing and I need an extra pair of eyes or an external opinion maybe.

I hesitated to post a question here, but I’ve already searched and tried as much as I could, and I thought it’s better to ask for help rather than abandoning it.

Ok, I’ve figured it out. I was doing a couple of silly things and wasn’t looking at the problem clearly.

What helped me realize the errors was the assertion message:

``````AssertionError: 'Perc[21 chars]ory\n\n100|          \n 90|          \n 80|   [357 chars]  \n' != 'Perc[21 chars]ory\n100|          \n 90|          \n 80|     [353 chars] t  '
``````
1. There where two `\n` after the title of the chart. I was adding the new line on the creation of the string and then as part of my loop.
2. I was doing a similar thing with the `\n` characters at the end of the axis labels. The expected solution doesn’t have a new line at the end, it ends with `... t '` (a space)
3. I was rounding out my percentages using
`percentages = [int(round(el / sum(totals) * 100, -1)) for el in totals] `
This messed up with the heights of the bars. I removed the rounding and int casting and not it works fine!

So I passed this test!

2 Likes

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