Build a Budget App Project - chrome console test AssertionError outputs

Tell us what’s happening:

I’m trying to make sense of the console output for the tests, I’m on the final function and it seems there is either an extra space or a missing space that is being hidden at ends of lines perhaps, but either way I can’t figure out how to ascertain that from the console output.

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
        else:
            return False

    def get_balance(self):
        balance = 0
        for transaction in self.ledger:
            balance += transaction['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):
        return amount <= self.get_balance()

    def __str__(self):
        line_length = 30
        output = ''
        for i in range((line_length - len(self.name)) // 2):
            output += '*'
            line_length -= 1
        output += f'{self.name}'
        line_length -= len(self.name)
        for i in range(line_length):
            output += '*'
        output += '\n'
        for transaction in self.ledger:
            spaces = 30
            if len(transaction['description']) > 23:
                output += f"{transaction['description'][0:23]}"
                spaces -= 23
            else:
                output += f"{transaction['description']}"
                spaces -= len(transaction['description'])
            spaces -= len(f"{float(transaction['amount']):.2f}")
            for i in range(spaces):
                output += ' '
            output += f"{float(transaction['amount']):.2f}"
            output += '\n'
        output += f"Total: {float(self.get_balance()):.2f}"
        return output

def create_spend_chart(categories):
    percentages = []
    total = 0
    for category in categories:
        spent = 0
        for item in category.ledger:
            if item['amount'] < 0:
                total += abs(item['amount'])
                spent += abs(item['amount'])
        percentages.append({'name': category.name, 'spent': spent})
    for item in percentages:
        item['percentage'] = item['spent'] * 100 // total
    output = ''
    output += 'Percentage spent by category\n'
    i = 100
    while i >= 0:
        output += f"{' ' if i < 100 else ''}{' ' if i == 0 else ''}{i}|"
        for item in percentages:
            if item['percentage'] >= i:
                output += ' o '
            else:
                output += '   '
        output += ' \n'
        i -= 10
    output += '    '
    for i in range(len(percentages)):
        output += '---'
    output += '-\n'
    names = []
    for item in percentages:
        names.append(len(item['name']))
    for i in range(max(names)):
        output += ('    ')
        for item in percentages:
            output += (f" {item['name'][i] if i < len(item['name']) else ' '} ")
        output += '\n'
    return output

# setUp (line 12, VM382)
food = Category("Food")
entertainment = Category("Entertainment")
business = Category("Business")

# test_create_spend_chart(line 17, VM382)
food.deposit(900, "deposit")
entertainment.deposit(900, "deposit")
business.deposit(900, "deposit")
food.withdraw(105.55)
entertainment.withdraw(33.40)
business.withdraw(10.99)

print(create_spend_chart([business, food, entertainment]))

Your browser information:

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

Challenge Information:

Build a Budget App Project - Build a Budget App Project

Welcome to the forum @axtonsmith

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!

Thank you, brother. That was a really thorough explanation, fantastic. Solved the two really simple issues in less than a minute thanks to you!

1 Like