Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

My codes have past all the tests except the very last one:

24. create_spend_chart should print a different chart representation. Check that all spacing is exact. Open your browser console with F12 for more details.

I am not familiar with reading the tests from the browser console. It would be greatly appreciated if anyone could point out what is wrong.

Your code so far

class Category:
    def __init__(self, cat):
        self.name = cat
        self.ledger = []
        self.spending = 0
    def __str__(self):
        output = ''
        output += f'{self.name.center(30, "*")}\n'
        for item in self.ledger:
            output += f"{item['description'][:23]:<23}"
            output += f"{item['amount']:>7.2f}\n"
        output += f'Total: {self.get_balance()}'
        return output
    def deposit(self, amount, description = ''):
        self.ledger.append({'amount': amount, 'description': description})
    def get_balance(self):
        balance = 0
        for item in self.ledger:
            balance += item['amount']
        return balance
    def check_funds(self, amount):
        return False if amount > self.get_balance() else True
    def withdraw(self, amount, description = ''):
        if self.check_funds(amount):
            self.ledger.append({'amount': - amount, 'description': description})
            self.spending += amount
            return True
        else:
            return False
    def transfer(self, amount, cat):
        if self.check_funds(amount):
            self.withdraw(amount, f'Transfer to {cat.name}')
            cat.deposit(amount, f'Transfer from {self.name}')
            return True
        else:
            return False

def create_spend_chart(categories):
    total = 0
    height = {}
    name_lens = []
    for cat in categories:
        total += cat.spending
        name_lens.append(len(cat.name))
    for cat in categories:
        height[cat.name] = (((cat.spending / total)*100)//10)*10
    num_lines = []
    for i in range(11):
        num_lines.append(f'{i*10:>3}|')
    axis = '    '
    for cat in categories:
        for i in range(11):
            num_lines[i] += ' o ' if height[cat.name] >= i else '   '
        axis += '---'
        if cat == categories[-1]:
            for i in range(11):
                num_lines[i] += ' \n'
            axis += '-\n'
    reversed_lines = num_lines[::-1]
    chart = 'Percentage spent by category\n'
    for line in reversed_lines:
        chart += line
    chart += axis
    max_len = max(name_lens)
    name_lines = []
    for i in range(max_len):
        name_lines.append('    ')
    for i in range(max_len):
        for cat in categories:
            if i < len(cat.name):
                name_lines[i] += f' {cat.name[i]} '
            else:
                name_lines[i] += '   '
        if cat == categories[-1]:
            if i != max_len - 1:
                name_lines[i] += ' \n'
            else:
                name_lines[i] += ' '
        chart += name_lines[i]
    print(chart)
    return(chart)

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36

Challenge Information:

Build a Budget App Project - Build a Budget App Project

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!