Build a Budget App - Build a Budget App

Tell us what’s happening:

I don’t understand why I am failing the last two test. Can someone take a look and tell me why I am failing these test?

Your code so far

class Category:
    def __init__(self, name):
        self.name = name
        self.ledger = []
    
    def __str__(self):
        result = self.name.center(30, '*') + '\n'
        for item in self.ledger:
            description = item['description'][:23]
            amount = f'{item["amount"]:7.2f}'
            result += f"{description:<23}{amount}\n"
        result += f"Total: {self.get_balance():.2f}"
        return result

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

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

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

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

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

    percentages = [(int((s / total) * 100) // 10) * 10 for s in spent]

    chart = "Percentage spent by category\n"

    for level in range(100, -1, -10):
        chart += f"{level:>3}| "
        for percent in percentages:
            if percent >= level:
                chart += "o  "
            else:
                chart += "   "
        chart += "\n"

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

    max_len = max(len(cat.name) for cat in categories)
    for i in range(max_len):
        chart += "     "
        for cat in categories:
            if i < len(cat.name):
                chart += cat.name[i] + "  "
            else:
                chart += "   "
        chart += "  \n"

    return chart.rstrip()

Your browser information:

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

Challenge Information:

Build a Budget App - Build a Budget App

There should be a more detailed error in the browser console (F12)

The assertion error and diff 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

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------    -----'

The 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.

I hope this helps interpret your error!