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
Your browser information:
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
Link to the challenge:
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.