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: