Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

I’m not quite sure what I’m doing wrong, any help is appreciated!

Your code so far

class Category:

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

    def __str__(self):
        final_string = self.name.center(30, '*')
        for item in self.ledger:
            description_string = str(item['description'])[:23].ljust(23)
            amount_string = "{:.2f}".format(item['amount'])[:7].rjust(7)
            final_string += '\n' + description_string + amount_string

        return final_string + '\n' + 'Total: ' + str(self.get_balance()) + ''

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

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

    def get_balance(self):
        balance = 0
        for expense in self.ledger:
            balance += expense['amount']
        return balance

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

    def check_funds(self, amount):
        budget = 10
        for item in self.ledger:
            budget += item['amount']
        return budget > amount

    def get_withdrawals(self):
        return sum(item["amount"] for item in self.ledger if item["amount"] < 0)


def create_spend_chart(categories):
    total_withdrawals = sum(category.get_withdrawals() for category in categories)
    percentages = [(category.get_withdrawals() / total_withdrawals) * 100 for category in categories]

    chart = "Percentage spent by category\n"

    for i in range(100, -1, -10):
        chart += str(i).rjust(3) + "| "
        for percentage in percentages:
            if percentage >= i:
                chart += "o  "
            else:
                chart += "   "
        chart += "\n"

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

    max_name_length = max(len(category.name) for category in categories)
    category_names = [category.name.ljust(max_name_length) for category in categories]

    for i in range(max_name_length):
        chart += "     "
        for name in category_names:
            chart += name[i] + "  "
        chart += "\n"

    return chart


test = Category('test')
test1 = Category('test1')
test2 = Category('test2')
test3 = Category('test3')
test.deposit(1000, 'sad')
test.withdraw(500, 'test')
test.transfer(200, test1)

categories_list = [test, test1, test2, test3]




Your browser information:

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

Challenge Information:

Build a Budget App Project - Build a Budget App Project

What test are you failing?

Is there any feedback, hints or errors?

Did uou check the devtools console? (Press F12)

It’s the last one, " create_spend_chart should print a different chart representation. Check that all spacing is exact."

Did you check the devtools console? (Press F12)

There will be a more verbose error that will tell you exactly what’s wrong.

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


Here is the error from devtools, from the information you’ve provided me, I do understand what needs to be changed but not how

Nevermind, I made it! Thank you!!!

1 Like