A little issue with Budget App Project

Hello there! So after finishing my project on Replit, I get an error feedback (down below).
But before that, here’s how I wrote my create_spend_chart() function:

def create_spend_chart(categories):
    
    total_withdrawals = sum(category.get_withdrawals() for category in categories)
    
    percentages = []
    for category in categories:
        percentage = (category.get_withdrawals() / total_withdrawals) * 100
        percentages.append(percentage)

    # creating chart 
    chart = 'Percentage spent by category\n'
    
    for num in reversed(range(0, 110, 10)):
        chart += str(num).rjust(3) + '|'
        for category, percentage in zip(categories, percentages):
            if num <=  percentage:
                chart += " o "
            else:
                chart += "  "
        chart += '\n'
    chart += (' ' * 4) + ("---" * len(categories)) + '-' + '\n'
    
    # printing out the names vertically
    max_name_length = max(len(category.category) for category in categories)

    chart += ' ' * 2 # Add initial spacing before the vertical names

    for i in range(max_name_length):
        chart += ' ' * 2  # Add spacing between the vertical names and the chart bars
        for category in categories:
            if i < len(category.category):
                chart +=  ' ' + category.category[i] + ' '   # Adjust the spacing between the characters
            else:
                chart += '   '  # Add empty spaces when the category name is shorter
        chart += '\n' + (' ' * 2)  # Move to the next line after printing all the characters
        
    return chart 

And the console returned this error( Even though the output is physically identical to the expected output):

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|      
 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/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[31 chars]     \n 90|      \n 80|      \n 70|   o   \n 6[302 chars]\n  ' != 'Perc[31 chars]         \n 90|          \n 80|          \n 70[343 chars] t  '
  Percentage spent by category
- 100|      
+ 100|          
?           ++++
-  90|      
+  90|          
?           ++++
-  80|      
+  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 
+   0| o  o  o  
?              +
      ----------
-      B  F  E 
+      B  F  E  
?              +
-      u  o  n 
+      u  o  n  
?              +
-      s  o  t 
+      s  o  t  
?              +
-      i  d  e 
+      i  d  e  
?              +
-      n     r 
+      n     r  
?              +
-      e     t 
+      e     t  
?              +
-      s     a 
+      s     a  
?              +
-      s     i 
+      s     i  
?              +
-            n 
+            n  
?              +
-            m 
+            m  
?              +
-            e 
+            e  
?              +
-            n 
+            n  
?              +
-            t 
?              ^
+            t  ?              ^
-    : Expected different chart representation. Check that all spacing is exact.

----------------------------------------------------------------------
Ran 11 tests in 0.012s

FAILED (failures=1)
>

Can anyone please tell me what the issue is?
(I suspect I’m not writing the function the way they want me to, but what I wrote works physically, but ended up producing more characters than expected. Please share your opinion)

In the test output:

  • lines starting with - are actual lines returned by function
  • lines starting with + are expected by the test
  • lines starting with ? indicate the specific places in line, where difference occurs
2 Likes

I think I get it now. Thanks for the explanation. I’ll check it again and see if it works

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.