Build a Budget App - Build a Budget App

Tell us what’s happening:

Hello, I think I completed the challenge but it won’t pass test 20, 23 and 24, can someone see where something is wrong?

Your code so far

class Category:
    def __init__(self, name):
        self.name = name
        self.ledger = []
        self.balance = 0

#Tilføj transaction til listen

    def deposit(self, amount, description=''):
        self.balance += amount
        self.ledger.append({'amount': amount, 'description': description})

#Træk fra kontoen

    def withdraw(self, amount, description=''):
        if self.check_funds(amount):
            self.deposit(-amount, description)
            return True
        else:
            return False

    def get_balance(self):
        return self.balance

    def transfer(self, amount, another_category):
        if self.check_funds(amount):
            self.balance -= amount
            self.ledger.append({'amount': -amount, 'description': f'Transfer to {another_category.name}'})
            another_category.deposit(amount, f'Transfer from {self.name}')
            return True
        else:
            return False

    def check_funds(self, amount):
        if amount <= self.balance:
            return True
        else:
            return False

    def __str__(self):
        output = self.name.center(30,'*') + '\n'
        for item in self.ledger:
            amount = item['amount']
            description = item['description']
            output += f'{description[0:23]:23}{amount:7.2f}'
            output += '\n'
        
        output += 'Total: '
        output += format(self.balance, '.2f')

        return output
        


def create_spend_chart(categories):
    categories_name = []
    categories_procent = []
    chartstr = 'Percentage spent by category\n'

#beregning af totalt brugt for kategori
    total_withdrawal = 0
    tot_spend = []

    for category in categories:
        categories_name.append(category.name)
        cat_spend = []
        for item in category.ledger:
            if item['amount'] < 0:
                total_withdrawal += abs(item['amount'])
                cat_spend.append(abs(item['amount']))
        cat_tot_spend = sum(cat_spend)
        tot_spend.append(cat_tot_spend)
    
#Beregning af procentdele
    for spend in tot_spend:
        cat_procent = 0
        cat_procent = spend/total_withdrawal*100
        categories_procent.append(cat_procent)
            

#Lav grafen
    for num in range(100, -10, -10):
        chartstr += f"{str(num) + '|':>4}"
        for procent in categories_procent:
            if procent >= num:
                chartstr += ' o '
            else:
                chartstr += '   '
        chartstr += '\n'
    chartstr += '    ' + (3*len(categories)+1)*'-' + '\n'


#Indsæt kategorinavne
    max_length = max(len(category) for category in categories_name)
    for i in range(0, (max_length + 1)):
        lodret_navne = '     '
        for category in categories_name:
            if i < len(category):
                lodret_navne += category[i] + '  '
            else:
                lodret_navne += '   '
        lodret_navne += '\n'
        
        chartstr += lodret_navne 
        output = chartstr.strip()
    
    return output


food = Category('Food')
food.deposit(1000, 'initial deposit')
clothing = Category('Clothing')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
food.transfer(50, clothing)
clothing.withdraw(30,'Trousers')
auto = Category('Auto')
auto.deposit(400, 'Initial deposit')
auto.withdraw(15, 'New wheels')

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15

Challenge Information:

Build a Budget App - Build a Budget App

let’s take a look at what we find in the browser console (as written in the note, this challenge has more output of the tests in the browser console)

AssertionError: 'Perc[34 chars]     \n 90|         \n 80|         \n 70|    o[327 chars]   t'
             != 'Perc[34 chars]      \n 90|          \n 80|          \n 70|  [340 chars] t  '

it looks like you have a spacing issue
the first line is your code the second line is the expected

I figured out how to open the console but I don’t really know how to interpret what the console shows?:thinking:

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!