Build a Budget App -- Solution didn't pass tests

Hi everyone, this is my solution:

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

    def __str__ (self):
        title =, '*')
        text = ''

        for trx in self.ledger:
            desc = trx['description'][:23].capitalize().ljust(23)
            amount = '{:.2f}'.format(trx['amount'])[-7:]
            max_len = 30            
            text += desc + str(amount).rjust(max_len - len(desc)) + '\n'

        text += f'Total: {self.get_balance()}'
        return f"{title}\n{text}"

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

    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):
        self.balance = 0
        for item in self.ledger:
            self.balance += item['amount']
        return self.balance

    def check_funds (self, amount):
        if self.get_balance() < amount:
            return False
        return True
    def transfer (self, amount, destination):
        if self.check_funds(amount):        
            self.withdraw(amount, f"Transfer to {destination.budget}")
            destination.deposit(amount, f"Transfer from {self.budget}")
            return True
        return False
def create_spend_chart(categories):
    income_by_category = []
    spent_by_category = []
    for category in categories:
        spent = 0
        income = 0
        for item in category.ledger:
            if item['amount'] < 0:
                spent += -item['amount']
                income += item['amount']

    percentages = [(spent/income)*100 for spent, income in zip(spent_by_category, income_by_category)]

    chart = "Percentage spent by category\n"
    for i in range(100, -10, -10):
        chart += str(i).rjust(3) + '|'

        for percentage in percentages:
            if percentage >= i:
                chart += " o "
                chart += "   "
        chart += '\n'

    chart += "    " + "-" * (len(categories) * 3 + 1) + "\n"

    for i in range(max(len(category.budget) for category in categories)):
        chart += "     "
        for category in categories:
            if i < len(category.budget):
                chart += category.budget[i] + "  "
                chart += "   "
        chart += "\n"
    return chart

My chart looks the same as the one given in the example. However, it doesn’t pass the last two test cases:

  • Failed:Printing a Category instance should give a different string representation of the object.
  • Failed:create_spend_chart should print a different chart representation. Check that all spacing is exact.

Any idea of the reason?

1 Like

Check the console tools (f12 to bring them up) Console area for a more detailed and verbose error message.

It will tell you exactly how your output differs from the expected output (often a spacing issue)

1 Like

Please don’t post solution code :+1:

And welcome to the forum!

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.

1 Like