Build a Budget App - Build a Budget App

Tell us what’s happening:

How can i fix this code? I failed at tests 21 and 24.
Anyone know which side I should fix?

Your code so far

import math


class Category:
    def __init__(self, name):
        self.name = name
        self.ledger = []

    def deposit(self, amount, description=''):
        self.ledger.append({'amount': amount, 'description': description})

    def withdraw(self, amount, description=''):
        if self.check_funds(amount):
            self.ledger.append({'amount': -amount, 'description': description})
            return True
        return False

    def get_balance(self):
        return sum(item['amount'] for item in self.ledger)

    def transfer(self, amount, destination):
        if self.check_funds(amount):
            self.withdraw(amount, f'Transfer to {destination.name}')
            destination.deposit(amount, f'Transfer from {self.name}')
            return True
        return False

    def check_funds(self, amount):
        return amount <= self.get_balance()

    def __str__(self):
        title = self.name.center(30, '*')
        lines = [title]
        for item in self.ledger:
            desc = item['description'][:23].ljust(23)
            amount = f"{item['amount']:.2f}"[:7].rjust(7)
            lines.append(f"{desc}{amount}")
        lines.append(f"Total: {self.get_balance():.2f}")
        return '\n'.join(lines)


def create_spend_chart(categories):
    withdrawals = []
    for cat in categories:
        total = sum(-item['amount'] for item in cat.ledger if item['amount'] < 0)
        withdrawals.append(total)

    grand_total = sum(withdrawals)
    percentages = [math.floor(w / grand_total * 100 / 10) * 10 for w in withdrawals]

    lines = ['Percentage spent by category']

    for level in range(100, -1, -10):
        row = f"{level:3}| "
        for pct in percentages:
            row += 'o  ' if pct >= level else '   '
        lines.append(row)

    # Horizontal line: 4 spaces + dash for each category (3 each) + 2 extra
    lines.append('    -' + '---' * len(categories) + '-')

    # Vertical category names
    max_len = max(len(cat.name) for cat in categories)
    for i in range(max_len):
        row = '     '
        for cat in categories:
            row += (cat.name[i] if i < len(cat.name) else ' ') + '  '
        lines.append(row)

    return '\n'.join(lines)

Your browser information:

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

Challenge Information:

Build a Budget App - Build a Budget App

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-budget-app/5e44413e903586ffb414c94e.md at main · freeCodeCamp/freeCodeCamp · GitHub

this lab has extra output in the browser console

AssertionError: 'Perc[211 chars]------\n     B  F  E  \n     u  o  n  \n     s[164 chars] t  '
             != 'Perc[211 chars]-----\n     B  F  E  \n     u  o  n  \n     s [163 chars] t  '

the first line is yours, the second is the expected

there is also this visual showing it:

You’re failing tests 21 and 24 because of two small bugs in create_spend_chart:

  1. Extra dash in the horizontal line – You added a final dash that shouldn’t be there. Change:
    removed by moderator

  2. Division by zero – If total withdrawals are 0, your code crashes. Add a check:

removed by moderator

Fix these and both tests will pass.
Recommendation: Always test edge cases like zero spending, and double‑check exact formatting from the challenge examples. If you want to download related to the tutorial video in youtube use yt1d website

Welcome to the forum @arjun_rogers88!

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge. How to Help Someone with Their Code Using the Socratic Method

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Happy coding!