Scientific Computing with Python Projects - Budget App - Spacing

Tell us what’s happening:
Describe your issue in detail here.

My code fails the ‘test_create_spend_chart’ test, and I am not sure why. I believe it has to do with spacing, but I don’t understand the error message. Any help would be greatly appreciated!

Your code so far

> def create_spend_chart(categories):
> 
>     category_index = (len(categories) - 1)
>     category_list = []
>     
>     def calculate_percentage(category_index):
>         if category_index < 0:
>             return category_list
>         else:
>             category = categories[category_index].category
>             expense_sum = 0
> 
>             for i in categories[category_index].ledger:
>                 if i["amount"] < 0:
>                     expense_sum += round(abs(i["amount"]) / 10) 
>             category_list.append({"category":category, "amount":round(expense_sum, -1)})                 
>             return calculate_percentage(category_index - 1)
>         
>     calculate_percentage(category_index)
> 
>     # Sort list so that chart iteration prints greatest to least amount
>     sorted_list = sorted(category_list, key=lambda i: i["amount"], reverse=True)
>     chart = f'Percentage spent by category\n'
>     dashes = ""
>     
>     for i in range(100,-1,-10):
>         chart += f'{i:>{3}}| '
>         for cat in sorted_list:
>             percentage = cat["amount"]
>             if percentage >= i:
>                 chart += f'o  '
>         chart += "\n"
>     
>     # For each category, there are 3 dashes
>     # Add one additional dash for the spacing between first bar of chart and y-axis
>     dashes += "___" * (len(sorted_list)) + "_"
>     chart += f'{dashes:>{len(dashes) + 4}}\n'
> 
>     max_letter_count = 0
>     for c in categories:
>         if len(c.category) > max_letter_count:
>             max_letter_count = len(c.category)
>     
>     line_letters = ""
>     for k in range(0, max_letter_count):
>         for item in sorted_list:
>             if len(item["category"]) > k:
>                 letter = item["category"][k]
>                 line_letters += f'{letter}  '
>             else:
>                 line_letters += "   "
>         chart += f'     {line_letters}\n'
>         line_letters = ""
>     print(chart)
>     return chart

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36

Challenge: Scientific Computing with Python Projects - Budget App

Link to the challenge:

Can you paste in the error message?

If you look at the assert statement in the error, it will show you the output string you made and the one it expects. You will be able to tell if there is a spacing issue there. It helpfully leaves out a lot of the string and shows you only the parts where there are discrepancies.

1 Like

Hey, thanks for responding @pkdvalis ! Here is the assertion error I receive when trying to run my code

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[26 chars]100| \n 90| \n 80| \n 70| \n 60| \n 50| \n 40|[263 chars]  \n' != 'Perc[26 chars]100|          \n 90|          \n 80|          [348 chars] t  '

So when you compare the two strings in the assert:

actual (your output):
'Perc[26 chars]100| \n 90| \n 80| \n 70| \n 60| \n 50| \n 40|[263 chars]  \n'
expected:
'Perc[26 chars]100|          \n 90|          \n 80|          [348 chars] t  '

You can see that you are missing extra spaces on each line,you need to fill in spaces to complete the line to the end of the text block. You also have a \n at the very end that needs to be removed.

1 Like

Thank you @pkdvalis ! I’ll work on that right now

1 Like

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