Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

According to this I’m missing the last one, but I revised a few times and I got the exact same. Any suggestions? Thanks

Your code so far

class Category:
    
    def __init__(self, cat):
        self.cat = cat
        self.name = cat
        self.ledger = []
        return
    
    def deposit(self, amount, description=''):
        self.ledger.append({'amount': amount, 'description': description})
        return

    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):
        total = 0
        for i in range(len(self.ledger)):
            total += float(self.ledger[i]['amount'])
        return total
    
    def transfer(self, amount, cat):
        if self.check_funds(amount):
            text_w = 'Transfer to ' + cat.name
            self.withdraw(amount, text_w)
            text_d = 'Transfer from ' + self.name
            cat.deposit(amount, text_d)
            return True
        else:
            return False

    def check_funds(self, amount):
        if amount > self.get_balance():
            return False
        else:
            return True
    
    def __str__(self):
        text = f'{self.cat:*^30}\n'
        for i in range(len(self.ledger)):
            descr = f"{self.ledger[i]['description'][:23]}"
            text += descr
            length = 30 - len(descr)
            text += f"{(self.ledger[i]['amount']):.2f}".rjust(length) + f'\n'
        text += f'Total: {self.get_balance():.2f}'
        return text


def create_spend_chart(categories):
    text = f'Percentage spent by category\n'
    lines = []
    values = []
    names = []
    length_name = []
    displayed_names = []

    
    for category in categories:
        values.append(category.get_balance())
        names.append(category.name)
        length_name.append(len(category.name))
        longest_name = max(length_name)
    
    n = 100
    total_values = sum(i for i in values)
    percentages = [round(i*10 / total_values) for i in values]
    for bars in range(11):
        lines.append(f'{n:>3}| ')
        n -= 10
        for qt in percentages:
            if qt > (n/10):
                lines[bars] += 'o  '
            else:
                lines[bars] += '   '
        
        text += f'{lines[bars]}\n'

    text += '    ' + '-'*longest_name + '--'

    for i in range(longest_name):
        displayed_names.append('     ')
        for name in names:
            if i < len(name):
                displayed_names[i] += f'{name[i]}  '
            else:
                displayed_names[i] += '   '
            
                
        text += f'\n{displayed_names[i]}'
    
    return text





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')
food.transfer(50, clothing)
auto = Category('Auto')
auto.deposit(2500, 'brrumm brrumm')
auto.withdraw(89.95, 'changing tires')

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/129.0.0.0 Safari/537.36

Challenge Information:

Build a Budget App Project - Build a Budget App Project

Did you press F12 and check the browser console errors?

I tried but I’m not really sure how to use it :sweat_smile: Could you give me a short explanation about how to use that tool and look for my own mistakes, please?

1 Like

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 so much! I will definetly try to solve my mistakes that way. Actually, I’ve fixed a couple of errors. But I still have a few questions:
What does a dash or dashes after a question mark mean?? Like ‘? —’
Also, is there a way to know what information is trying to pass the test through the function? Because it’s seems my problem is that I’m not managing the values correctly, and I can’t see why
Thank you in advance for your explanations

Can you share your error here please. I prefer to work with this format of errro in any case I find it a bit more clear:

'  3801      123    \n   - 2     + 49    \n------    -----    \n'
'  3801      123\n-    2    +  49\n------    -----'

Can you copy and paste it? It’s hard to work with a screenshot.

It looks like a calculation error, since your output (shown first) has no o and the expected output (second) has an o before \n 60|

I would check your rounding and percentage calculations.

The percentage spent should be calculated only with withdrawals and not with deposits.

The height of each bar should be rounded down to the nearest 10.

Ohh! You’re right, I’m not taking spents but deposits too. Thank you, I’ll fix that

1 Like