Build a Budget App - Build a Budget App

Tell us what’s happening:

Cases 19, 20, 22, & 24 are flagged as errors.
My main issue is calculating percentages to be rounded up to the nearest 10, I think from there everything else will be an easier fix.

Your code so far

class Category:
    def __init__(self, name):
        self.name = name
        self.ledger = []
        self.balance = 0
    
    def deposit(self, amount, description = ''):
        records = {'amount': amount, 'description': description}
        self.balance += amount
        self.ledger.append(records)
    
    def withdraw(self, amount, description = ''):
        if self.check_funds(amount):
            amount = amount * -1
            self.balance += amount
            records = {'amount': amount, 'description': description}
            self.ledger.append(records)
            return True
        else:
            return False
            
    def get_balance(self):
        return self.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):
        if self.balance < amount:
            return False
        else:
            return True

    def __str__(self):
        ledger_title = self.name.center(30, "*")
        ledger_sections = ''

        for entry in self.ledger:
            descriptions = entry['description']
            amounts = entry['amount']
            if len(descriptions) > 23:
                descriptions = descriptions[0:23]
            amounts = f'{amounts:.2f}'
            descriptions = f'{descriptions:.23}'
            ledger_sections += f'{descriptions:23}{amounts:>7}\n'

        return f'{ledger_title}\n{ledger_sections}Total: {self.balance:.2f}'
            
def create_spend_chart(categories):
    total_spent = 0
    category_total = []
    category_amount = len(categories)
    percentages = []
    chart_title = 'Percentage spent by category\n'
    chart = ''
    graph = ''
    names = ''

    for category in categories:
        temp_total = 0
        for entry in category.ledger:
            if entry['amount'] < 0:
                total_spent += abs(entry['amount'])
                temp_total += abs(entry['amount'])
        category_total.append(temp_total)

    percentages = [round(num / total_spent * 10) * 10 for num in category_total]

    for i in range(100, -10, -10):
        for _ in range(category_amount):
            if percentages[_] == i:
                graph += 'o  '
        chart += f'{i:3}| {graph}\n'
        if i == 0:
            chart += '    ' + ('-' * (category_amount * 3)) + '-'

    max_chars = max(len(category.name) for category in categories)
   
    for i in range(0, max_chars):
        names += "     "
        for category in categories:
            if len(category.name) > i:
                names += category.name[i] + "  "
            else:
                names += "   "
        names += "\n"

    return f'{chart_title}{chart}\n{names}'

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:149.0) Gecko/20100101 Firefox/149.0

Challenge Information:

Build a Budget App - Build a Budget App

Welcome back @Smeef !

Open your browser’s console and check the assertion errors after you run the tests:

'Perc[26 chars]100| \n 90| \n 80| \n 70| o  \n 60| o  \n 50| [290 chars]  \n' != 
'Perc[26 chars]100|          \n 90|          \n 80|          [348 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!


Happy coding!

1 Like