Stuck on the last 2 tests - Build a Budget App Project

Tell us what’s happening:

Hello, when finalizing my code I have two tests that fail and I can’t seem to understand why. Both errors, 23 and 24, tell me to fix my spacing as shown in the example output. I looked at my code and made sure all the requirements were met, double lines in between each category. I have asked AI to fix it but it still doesn’t work. Someone please help me I am very confused.

Your code so far

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, category):
        if self.check_funds(amount):
            self.withdraw(amount, f"Transfer to {category.name}")
            category.deposit(amount, f"Transfer from {self.name}")
            return True
        return False

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

    def __str__(self):
        # Title line of 30 characters (Centering the category name)
        title = f"{self.name:*^30}"
        
        # Ledger lines (description and amount)
        ledger_lines = ""
        for item in self.ledger:
            description = item['description'][:23]  # Only first 23 characters of description
            amount = f"{item['amount']:>7.2f}"  # Amount with 2 decimal places, right aligned
            ledger_lines += f"{description:<23}{amount}\n"

        # Total line
        total = self.get_balance()
        total_line = f"Total: {total:0.2f}"

        return title + "\n" + ledger_lines + total_line

def create_spend_chart(categories):
    # Calculate total spent
    total_spent = sum(
        sum(item['amount'] for item in category.ledger if item['amount'] < 0) for category in categories
    )

    # Calculate the percentage spent for each category
    percentages = []
    for category in categories:
        spent = sum(item['amount'] for item in category.ledger if item['amount'] < 0)
        if total_spent == 0:
            percentages.append(0)
        else:
            percentages.append(int(spent / total_spent * 100))

    # Generate the chart
    chart = "Percentage spent by category\n"
    
    # Create the chart from 100 down to 0
    for i in range(100, -1, -10):
        chart += f"{i:3}| "  # Add the percentage label
        for percentage in percentages:
            if percentage >= i:
                chart += "o  "
            else:
                chart += "   "
        chart += "\n"
    
    # Add the horizontal line with the correct number of dashes
    chart += "    " + "-" * (len(categories) * 3 + 1) + "\n"  # Horizontal line with three dashes per category and two extra spaces

    # Add category names vertically
    max_name_length = max(len(category.name) for category in categories)
    
    # For each row of vertical category names
    for i in range(max_name_length):
        chart += "     "  # Adjusting space before category names
        for category in categories:
            if i < len(category.name):
                chart += category.name[i] + "  "
            else:
                chart += "   "  # This ensures the spacing is uniform
        chart += "\n"
    
    # Remove the extra spaces at the end of the final row
    return chart.strip()

# Example usage:
food = Category('Food')
food.deposit(1000, 'initial deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')

clothing = Category('Clothing')
clothing.deposit(500, 'initial deposit')
clothing.withdraw(50, 'new shoes')

auto = Category('Auto')
auto.deposit(2000, 'initial deposit')
auto.withdraw(150, 'gas')

# Transfers between categories
food.transfer(50, clothing)

# Print individual categories
print(food)
print(clothing)
print(auto)

# Print the spend chart
categories = [food, clothing, auto]
print(create_spend_chart(categories))

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36

Challenge Information:

Build a Budget App Project - Build a Budget App Project

Welcome to the forum @ko9898

Note: open the browser console with F12 to see a more verbose output of the tests.

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

This is called a diff, and it shows you the differences between two files or blocks of code:

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

The x shows an extra new line at the end.

Happy coding