I’m stuck on the budget project. I have no idea where my code is failing in the test case. I don’t see any problem, maybe I missed something. Please help me out. Link to the full code
def create_spend_chart(categories):
spent = []
percentages = []
for category in categories:
spent.append(category.get_all_withdrawls())
for amount in spent:
percentage = round((amount/sum(spent)), 1)*100
percentages.append(percentage)
title = "Percentage spent by category\n"
graph = ""
for interval in range(100, -10, -10):
graph += str(interval).rjust(3) + "| "
for percent in percentages:
if int(percent) >= interval:
graph += "o "
else:
graph += " "*3
graph += "\n"
graph += " "*4 + "-"*((len(categories)*2)+4)
# graph += "\n" + " "*5
# find max name len
maxx_name_len = 0
maxx_name = None
# print(len(categories))
for category in categories:
name = category.category_name
if len(name) > maxx_name_len:
maxx_name_len = len(name)
maxx_name = name
# print(maxx_name_len)
# print(maxx_name)
vertical_names = ""
for i in range(maxx_name_len):
if i < maxx_name_len:
vertical_names += "\n" + " "*5
for category in categories:
name = category.category_name
if len(name) > i:
vertical_names += name[i] + " "*2
else:
vertical_names += " "*3
# graph = graph.rstrip() + "\n"
vertical_names = vertical_names.rstrip("\n")
return f"{title}{graph}{vertical_names}"
I have linked to the full code in the original post.
Spent is a list of the total amount spent in each category. The method get_all_withdrawls returns a single value for each category, which is appended to the list.
Console Output
[{'amount': 1000, 'description': 'initial deposit'}, {'amount': -10.15, 'description': 'groceries'}, {'amount': -15.89, 'description': 'restaurant and more food for dessert'}]
973.96
*************Food*************
initial deposit 1000.00
groceries -10.15
restaurant and more foo -15.89
Transfer to Clothing -50.00
Total: 923.96
***********Clothing***********
Transfer from Food 50.00
-25.55
Total: 24.45
Percentage spent by category
100|
90|
80|
70| o
60| o
50| o
40| o
30| o
20| o o
10| o o o
0| o o o
----------
F C A
o l u
o o t
d t o
h
i
n
g
.F.........
======================================================================
FAIL: test_create_spend_chart (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/budget-app-harshit/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[170 chars] 10| o o o \n 0| o o o \n ----------[204 chars] t ' != 'Perc[170 chars] 10| o o \n 0| o o o \n ----------[204 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 : Expected different chart representation. Check that all spacing is exact.
----------------------------------------------------------------------
Ran 11 tests in 0.003s
FAILED (failures=1)
The - line is your output and the + line is the expected. This is trying to tell you that your bars are wrong, specifically the Business one. If you compare yours to the expected on in the tests, you’ll see that it expects only the zero line to have a bar in the Business column.
The problem is in two places, and is due to the fact that the spec requires rounding the bar amounts down to the the nearest ten. First:
This is a regular round, which will round 5 and up, up. The business percent is 7%, which will round to 10%. Print the percentages before rounding to see. Second:
This is where I assume you are trying to round down, but since the numbers are already rounded and integers, this does not round down. It can’t change the 10 from earlier into the zero it should be.
The reason the other two are correct is because they rounded down in the initial round.
Thank you so much. This solution worked! I did not round to zero. I changed the code so it doesn’t round up. Were we allowed to import additional libraries? I know if it were the case, I could have used math.floor() to simply round down.