I am struggling to find the errors in my create_spend_chart function, I have tried o fix it but I am lost. Can someone please help me.
This is my code:
def create_spend_chart(categories):
# calculating percent
p_dct = []
for cate in categories:
total = 0
p_amt = 0
for i in range(len(cate.ledger)):
nmbr = str(cate.ledger[i]["amount"])
if "-" in nmbr:
n = nmbr.split("-")
p_amt += float(n[1])
else:
total+= float(nmbr)
percenage = round((p_amt / total) * 100)
round_p = (percenage // 10) * 10
if round_p > 100:
round_p = 100
#elif round_p == 0:
# round_p = 10
p_dct.append({"percenage": round_p, "description": cate.name})
p_str = 'Percentage spent by category\n'
y_axis = [100,90,80,70,60,50,40,30,20,10,0]
# setting up string on the y_axis
for digit in y_axis:
if len(str(digit))== 3:
p_str+= str(digit).rjust(3) + '|'
elif len(str(digit)) == 0:
p_str += str(digit).rjust(3) + ' |'
else:
p_str += str(digit).rjust(3) + '|'
for i in range(len(p_dct)):
num1 = p_dct[i]["percenage"]
if num1 == digit:
p_str += ' o '
elif num1 >= digit:
p_str += ' o '
elif num1 < digit:
p_str += ' '
p_str+= ' \n'
# formating categories
d_len = (len(p_dct) * 3) + 1
p_str += ' ' + ('-' * d_len) + '\n'
str_lst = []
for i in range(len(p_dct)):
str_lst.append(p_dct[i]["description"])
max_length = max(len(item) for item in str_lst)
for i in range(max_length):
p_str+= ' '
for word in str_lst:
if i < len(word):
p_str += ' ' + word[i]
else:
p_str += ' '
p_str += '\n'
return p_str
The error:
FAIL: test_create_spend_chart (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-budget-app-1/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[35 chars] \n 90| \n 80| \n [359 chars] \n' != 'Perc[35 chars] \n 90| \n 80| \n 70| [339 chars] t '
Percentage spent by category
- 100|
? --
+ 100|
- 90|
? --
+ 90|
- 80|
? --
+ 80|
- 70|
- 60|
- 50|
- 40|
- 30|
- 20|
- 10| o
? ^ -
+ 70| o
? ^
+ 60| o
+ 50| o
+ 40| o
+ 30| o
+ 20| o o
+ 10| o o
- 0| o 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.
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15
Challenge: Scientific Computing with Python Projects - Budget App
Link to the challenge: