Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

Hi,
I cannot pass test #19. Checked it couple times and all cases tested by me seems to be ok. I don’t see any additional information in F12 console (this particular test does not give any). Also I’m not able to determine exact test case which causing failure.

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 get_balance(self):
        return sum([item['amount'] for item in self.ledger])

    def withdraws(self):
        return sum([abs(item['amount']) for item in self.ledger if item['amount'] < 0])
    
    def withdraw(self, amount, description = ''):
        if self.check_funds(amount):
            self.ledger.append(
                {
                    'amount' : (-1.0 * amount),
                    'description' : description
                }
            )
            return True
        else:
            return False

    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):

        s = '*' * (15 - len(self.name) // 2)
        s += self.name
        s += '*' * (30 - len(s))
        s += '\n'

        for item in self.ledger:
            desc = item['description']
            desc += ' ' * (23 - len(desc))
            s += desc[0:23]
            s += f"{item['amount']:.2f}".rjust(7, ' ')
            s += '\n'

        s+= 'Total: ' + f"{self.get_balance():.2f}"

        return s


def create_spend_chart(categories):
    s = 'Percentage spent by category\n'

    total_spent = sum(c.withdraws() for c in categories)
    
    test = list(((c.withdraws() / total_spent) * 100) for c in categories)

    print(test)

    ws = list((((c.withdraws() / total_spent) * 100) // 10) * 10 for c in categories)
    
    print(ws)

    line_length = 5 + len(categories) * 3

    for i in range(100, -1, -10):
        line = ''
        line += f'{i}|'.rjust(4, ' ')
        line += ' '
        
        cidx = 0
        for w in ws:
            if (w >= i):
                line += 'o  '

        s += line.ljust(line_length, ' ')        
        s += '\n'

    s += '    ' + '-' * (len(categories) * 3 + 1) + ''

    max_cat_len = max(len(c.name) for c in categories)

    for i in range(max_cat_len):
        s += '\n     '
        for c in categories:
            if (len(c.name) > i):
                s += c.name[i] + '  '
            else:
                s += '   '

    return s

food = Category('Food')
food.deposit(1000, 'deposit')
food.withdraw(600, 'groceries and dining')
clothing = Category('Clothing')
clothing.deposit(1000, 'deposit')
clothing.withdraw(200, 'clothing and accessories')
auto = Category('Auto')
auto.deposit(1000, 'deposit')
auto.withdraw(100, 'car repairs and gas')
print(create_spend_chart([food, clothing, auto]))


Your browser information:

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

Challenge Information:

Build a Budget App Project - Build a Budget App Project

Can you please show how you calculate the heights and explain how you round them down?

This list calculates percent rounded down to nearest ten.
Withdraws of each category divided by total (withdraws of all categories):

ws = list((((c.withdraws() / total_spent) * 100) // 10) * 10 for c in categories)

…it’s printed line below to check calculations.

And this loop:

for w in ws:
            if (w >= i):
                line += 'o  '

is responsible for printing ‘o’ if it is line on adequate level.

Maybe it would be easier if we could check the test data which is causing test failure.

Try adding this to the end of your code:

print(create_spend_chart([food, clothing, auto]))
print(create_spend_chart([clothing, auto, food]))

And look at the charts that are output. The text of the test is a bit mis-leading here, in that it may not be a rounding error. You can also check the console out output o the browser using F12 which will give you more error data to work with:

'Perc[74 chars] 70| o        \n 60| o        \n 50| o        [300 chars] t  ' != 
'Perc[74 chars] 70|    o     \n 60|    o     \n 50|    o     [300 chars] t  '

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!

That helped. Thanks.

1 Like