Hi All,
Would appreciate if any one could point out to me why the test modules print out “None” causing my code to fail the tests however when main.py is ran, the output seems correct.
Attached is my code
def create_spend_chart(categories):
# calculate expenses in each category
category_spend = []
for i in range (len(categories)):
spend_in_ledger = []
for j in range (len(categories[i].ledger)):
if categories[i].ledger[j]["amount"]<0:
spend_in_ledger.append(categories[i].ledger[j]["amount"])
total_spend_in_ledger = sum(spend_in_ledger)
category_spend.append(total_spend_in_ledger)
# calculate total expenses
total_spend = sum(category_spend)
# calculate % expenditure
category_percent = []
for i in range (len(category_spend)):
percent = (int(category_spend[i]/total_spend*10))*10
category_percent.append(percent)
# print out chart
print ("Percentage spent by category")
# generating a line on the chart
chart_line = []
yaxis = [x for x in range (100,-1,-10)]
for i in range (len(yaxis)):
chart = []
for j in range (len(category_percent)):
if yaxis[i] > category_percent[j]:
chart.append(" ")
else:
chart.append("o ")
chart_line.append("".join(chart))
# putting to gether the chart display
chart_display = ""
for i in range (len(yaxis)):
chart_display += f"{yaxis[i]:>3}" + "|" + " " + chart_line[i] + '\n'
chart_display = chart_display + " -" + ("---"*len(categories))
print (chart_display)
# printing xaxis category
cat_length = 0
cat_name = []
xaxis = ""
for i in range (len(categories)):
cat_name.append(list(categories[i].CategoryName))
if len(categories[i].CategoryName)> cat_length:
cat_length = len(categories[i].CategoryName)
for x in range(cat_length):
nameStr = " "
for i in range(len(cat_name)):
if x >= len(cat_name[i]):
nameStr += " "
else:
nameStr += cat_name[i][x] + " "
nameStr += '\n'
xaxis += nameStr
print (xaxis)
Attached is the test code
def test_create_spend_chart(self):
self.food.deposit(900, "deposit")
self.entertainment.deposit(900, "deposit")
self.business.deposit(900, "deposit")
self.food.withdraw(105.55)
self.entertainment.withdraw(33.40)
self.business.withdraw(10.99)
actual = create_spend_chart([self.business, self.food, self.entertainment])
expected = "Percentage spent by category\n100| \n 90| \n 80| \n 70| o \n 60| o \n 50| o \n 40| o \n 30| o \n 20| o o \n 10| o o \n 0| o o o \n ----------\n B F E \n u o n \n s o t \n i d e \n n r \n e t \n s a \n s i \n n \n m \n e \n n \n t "
self.assertEqual(actual, expected, 'Expected different chart representation. Check that all spacing is exact.')
Attached is the print screen of what is printed.