Scientific Computing with Python Projects - Budget App

Tell us what’s happening:

My code fails the test_create_spend_chart. It says to check the spaces but I can see any differences.
I realize it may be the way I calculate the percentage. Should we consider the percentage over all the categories or over the categories called by the function create_spend_chart ?

Your code so far

def create_spend_chart(categories: list):
## return an error if more than 4 categories
  if len(categories) > 4:
    return "Error: Too many categories"
  
  ## Calculate the total spending and spending per category
  total_spending = sum(sum(item['amount'] for item in category.ledger if item['amount'] < 0) for category in categories)
  category_spending = {category.category_name: -sum(item['amount'] for item in category.ledger if item['amount'] < 0) for category in categories}
  category_spending_list = list(category_spending.items())
 
  ## calculate the percentage spent in each category and store it in list of dictionnaries {"category_name" : "", "categoty_spendings" : float}
  
  category_spending_pc = [(item[0], int(item[1])*(-100 / total_spending)) for item in category_spending_list]
  #print(sorted_category_spending_pc)
  chart_width = 4 + len(categories) * 3 + 1
  chart_heigth = 12 + max(len(category) for category, _ in category_spending_pc)
  #build the lines of the chart
  my_string = ""  
  ##____Title_____
  title = "Percentage spent by category\n"
  
  my_string += title 
  ##____Body_____
  for i in range(100,-1,-10):
    my_string += str(i).rjust(3) + "|"
    for _element in category_spending_pc:
      if _element[1] >= i :
        my_string += " o "
      else:
        my_string += "   "
    my_string_formatted = "{:<{}}".format(my_string, chart_width)
    my_string = my_string_formatted + " \n"
  ##____Bottom_____
  len_bottom_line =(3 * len(categories) + 1) 
  my_string +=" " * 4 + "-" * len_bottom_line + "\n"
  
  max_len = max(len(category.category_name) for category in categories)
  #print(f"maximum len of categories names : {max_len}")
  ##____Labels_____
  for i in range(max_len):
    my_string += " " * 5
    #print(i)
    for _element in category_spending:
      if i < len(_element[0]):
        my_string += _element[0][i] + "  "
      else:
        my_string += "   "
    if i < max_len - 1:
      my_string += "\n"
  return my_string

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0

Challenge Information:

Scientific Computing with Python Projects - Budget App

Can you link your replit?

Hi @ madermx,

Scope of improvements in your code

  • Use this formula for accurate percent calculation (item[1] / total_spending) * 100.

  • When naming your variables, use uniform names; desist from using both category_spending as well as category_spending_list.

  • To make sure that you get accurate percentage values, remove int(item[1]).

The errors should point out where your extra spaces are. Here’s a primer on how to read them:

An assertion error gives you a lot of information to track down a problem. For example:

AssertionError: 'Year' != 'Years'
- Year
+ Years
?     +

Your output comes first, and the output that the test expects is second.

AssertionError: ‘Year’ != ‘Years’

Your output: Year does not equal what’s expected: Years

- Year
+ Years
?     +

- Dash indicates the incorrect output
+ Plus shows what it should be
? The Question mark line indicates the place of the character that’s different between the two lines. Here a + is placed under the missing s .

Here’s another example:

E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E       assert '  3801      123    \n   - 2     + 49    \n------    -----    \n' == '  3801      123\n-    2    +  49\n------    -----'
E         -   3801      123
E         +   3801      123    
E         ?                ++++
E         - -    2    +  49
E         +    - 2     + 49    
E         - ------    -----
E         + ------    -----    
E         ?                +++++

The first line is long, and it helps to view it as 2 lines in fixed width characters, so you can compare it character by character:

'  3801      123    \n   - 2     + 49    \n------    -----    \n'
'  3801      123\n-    2    +  49\n------    -----'

Again, your output is first and the expected output is second. Here it’s easy to see extra spaces or \n characters.

E         -   3801      123
E         +   3801      123    
E         ?                ++++

Here the ? line indicates 4 extra spaces at the end of a line using four + symbols. Spaces are a little difficult to see this way, so it’s useful to use both formats together.

I hope this helps interpret your error!

1 Like

I feel like my problem is in the percentage calculation… Here it is : boilerplate-budget-app - Replit

What error are you getting?

If it is with the percentages, make note of this instruction:

The percentage spent should be calculated only with withdrawals and not with deposits.

It’s a bit confusing but you need the percentage spent on a category of the total spent on all categories.

1 Like

Thanks for the heads up, and thank God I found this comment before wasting too much time on this last chunk of the project. That part of the instructions really needs a re-write.

1 Like

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