Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

I’ve been stuck in this project for a while, I’m not sure what should I fix.
I keep getting these issues :
19. The height of each bar on the create_spend_chart chart should be rounded down to the nearest 10.
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.

Your code so far

import math

class Category:
    def __init__(self, name):
        self.name = name
        self.ledger = []
        self.balance = 0
        
    def __str__(self):
        title_line = f"{self.name:*^30}\n"

        ledger_lines = ""
        for transaction in self.ledger:
            description = transaction['description'][:23]
            amount = f"{transaction['amount']:>7.2f}"
            ledger_lines += f"{description:<23}{amount}\n"
        total_line = f"Total: {self.balance:>.2f}"
        return title_line + ledger_lines + total_line 

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


    def withdraw(self, amount, description=''):
        if self.check_funds(amount):
            if not description:
                description = ''
            withdraw = {'amount' : -amount, 'description' : description}
            self.ledger.append(withdraw)
            self.balance -= amount
            return True
        else:
            return False
        
    
    def get_balance(self):
        return self.balance

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

    def check_funds(self, amount):
        return amount <= self.balance



def create_spend_chart(categories):
    total_spent = sum(
        sum(transaction['amount'] for transaction in category.ledger if transaction['amount'] < 0)
        for category in categories
    )

    category_spent = {
        category.name: sum(transaction['amount'] for transaction in category.ledger if transaction['amount'] < 0)
        for category in categories
    }

    
    percentages = {
        name: (int((spent / total_spent) * 100) // 10) * 10 if total_spent > 0 else 0
        for name, spent in category_spent.items()
    }
    
    
    chart = "Percentage spent by category\n"
    

    for percentage in range(100, -1, -10):
        chart += f"{percentage:>3}| "
        for category in categories:
            if percentages[category.name] >= percentage:
                chart += "o  "
            else:
                chart += "   "
        chart += "\n"
    
    
    chart += "    -" + "---" * len(categories) + "\n"
    
    
    max_name_length = max(len(category.name)for category in categories)
    
    for i in range(max_name_length):
        chart += "     "
        for category in categories:
            if i < len(category.name):
                chart += category.name[i] + "  "
            else:
                chart += "   "
        if i < max_name_length - 1:
            chart += "\n"
        

    return chart.rstrip("\n")

food = Category('Food')
food.deposit(900, 'deposit')  
food.withdraw(45.67, 'milk, cereal, eggs, bacon, bread')

clothing = Category("Clothing")
clothing.deposit(500, "deposit")
clothing.withdraw(150, "Jeans and shirts")

entertainment = Category("Entertainment")
entertainment.deposit(300, "deposit")
entertainment.withdraw(100, "Movies")
entertainment.withdraw(50, "Games")

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

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 Edg/131.0.0.0

Challenge Information:

Build a Budget App Project - Build a Budget App Project

did you do this? do you understand what you see?

I did

Do you have doubts or questions about it?


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!