Python budget App create spent chart

hi guys,

I need help on the Create_spend_chart function, when I test locally with the expected string from the test module, I get the same string, I test it using the == and I get True, so I am not sure why it doesn’t pass the test.

Here is my function (Sorry if it is messy!)

def create_spend_chart(categories):
percentages = [‘100’, ’ 90’, ’ 80’, ’ 70’, ’ 60’, ’ 50’, ’ 40’, ’ 30’, ’ 20’, ’ 10’, ’ 0’]

Creating a list in this forme: [(category1, total_spent), (category2, total_spent)…] where category is item of the categories instances.

Category =
total = 0
title = ‘Percentage spent by category\n’
y_axis =
for category in categories:
spent = 0
for v in category.ledger:
if float(v[‘amount’]) < 0:
spent += abs(v[‘amount’])
Category.append((category.category, spent))
#print(Category)

---- creating the Y axis -----

for category in Category:
total += category[1]
values =
for c in Category:
percent = round(c[1]/total*100)
values.append((c[0], percent))
for p in percentages:
line = p + ‘|’
for v in values:
if float(p) > v[1]:
line += ’ ’
else:
line += ’ o ’
y_axis.append(line + ’ ')

---- creating the separating line (dashes…) ----

separation = ’ ’ + ‘-’*(len(Category)*3+1)

---- creating the category names … -----

lengths = [len(c[0]) for c in Category]
m = max(lengths)
names =
for i in range(m):
line = ’ ’
for v in Category:
try:
line += f’ {v[0][i]} ’
except IndexError:
line += ’ ’
names.append(line + ’ ')

returning the result …

#print(type(title), type(’\n’.join(y_axis)), ‘\n’.join(names), sep=’\n’)

return title + ‘\n’.join(y_axis) + ‘\n’ + separation + ‘\n’ + ‘\n’.join(names)

Here is the error I get:

.F…

FAIL: test_create_spend_chart (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-budget-app/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[74 chars] 70| o \n 60| o \n 50| o [300 chars] ’ != 'Perc[74 chars] 70| o \n 60| o \n 50| o [300 chars] t ’
Diff is 1189 characters long. Set self.maxDiff to None to see it. : Expected different chart representation. Check that all spacing is exact.

If I enable the Diff I get some indications, but I am not really sure how to read that!

Percentage spent by category
100|
90|
80|

  • 70| o
    ? —
  • 70| o
    ? +++
  • 60| o
    ? —
  • 60| o
    ? +++
  • 50| o
    ? —
  • 50| o
    ? +++
  • 40| o
    ? —
  • 40| o
    ? +++
  • 30| o
    ? —
  • 30| o
    ? +++
  • 20| o o
    ? —
  • 20| o o
    ? +++
  • 10| o o
    ? —
  • 10| o o
    ? +++
    0| o o o

  •  F  E  B  
    

? —

  •  B  F  E  
    

? +++

  •  o  n  u  
    

? —

  •  u  o  n  
    

? +++

  •  o  t  s  
    

? —

  •  s  o  t  
    

? +++

  •  d  e  i  
    

? —

  •  i  d  e  
    

? +++

  •  n     r  
    
  •  e     t  
    
  •  s     a  
    
  •  s     i  
    
  •     r  n  
    

? ^

  •        n  
    

? ^

  •        m  
    
  •     t  e  
    

? ^

  •        e  
    

? ^

  •     a  s  
    
  •     i  s  
    
  •     n     
    

? —

  •        n  
    

? +++

  •     m     
    
  •     e     
    
  •     n     
    
  •     t     ?            ---
    
  •        t  ? +++
    

: Expected different chart representation. Check that all spacing is exact.

here is the test:

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)

This is the expected string:

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 "

Please help :innocent:

Thanks in advance :))

You’ll need to post in code blocks or post a link to a repl (on repl.it or somesuch). It’s too difficult to reformat before debugging.

1 Like

https://replit.com/@SliEl/budgetapp#main.py Here is the link, I appreciate the help

thank you in advance

Any idea guys about this one?

Still can’t figure that out :pensive:

That’s not the correct link. This one only links to a replit containing a single file, though I could navigate to your account and I assume this is the correct link: https://replit.com/@SliEl/boilerplate-budget-app

Now as for the error (just for the looks, explanation is below)

-  70| o        
?            ---
+  70|    o     
?      +++
-  60| o        
?            ---
+  60|    o     
?      +++
-  50| o        
?            ---
+  50|    o     
?      +++
-  40| o        
?            ---
+  40|    o     
?      +++
-  30| o        
?            ---
+  30|    o     
?      +++
-  20| o  o     
?            ---
+  20|    o  o  
?      +++
-  10| o  o     
?            ---
+  10|    o  o  
?      +++
    0| o  o  o  
      ----------
-      F  E  B  
?            ---
+      B  F  E  
?    +++
-      o  n  u  
?            ---
+      u  o  n  
?    +++
-      o  t  s  
?            ---
+      s  o  t  
?    +++
-      d  e  i  
?            ---
+      i  d  e  
?    +++
+      n     r  
+      e     t  
+      s     a  
+      s     i  
-         r  n  
?         ^
+            n  
?         ^
+            m  
-         t  e  
?         ^
+            e  
?         ^
-         a  s  
-         i  s  
-         n     
?            ---
+            n  
? +++
-         m     
-         e     
-         n     
-         t     
?            ---
+            t  
? +++

Now this looks like a mess but as far as I can tell, it seems like your categories are in the wrong order.
Looking at my solution: The order should be “Business, Food, Entertainment”, but yours is “Food, Entertainment, Business” → this might also cause the bars to appear in the wrong order.

1 Like

I changed the order of the categories inside the test file and now it works great

Ran 11 tests in 0.004s

OK

Thank you very much