Build a Budget App Project

Hello there! I can’t get the chart thing to pass the tests. I hope someone can take a look into it. Thank you and have a nice day.

def create_spend_chart(categories):
    spends = [sum(-item['amount'] for item in category.ledger if item['amount'] < 0) for category in categories]
    total_spent = sum(spends)
    percentages = [int(spend / total_spent * 10) * 10 for spend in spends]
    longest_name_length = max(len(category.name) for category in categories)
    chart = ""
    for i in range(100, -1, -10):
        line = f"{i:>3d}|"
        content = ''.join(" o " if percent >= i else "   " for percent in percentages)
        chart += f"{line}{content} \n"
    names = ""
    dashed_line = " "*4 + "-"*3*len(categories) + "-"
    for l in range(longest_name_length):
        line = "    "
        content = ''.join(f" {category.name[l]} " if len(category.name) > l else "   " for category in categories)
        names += f"{line}{content} \n"
    return f"Percentage spent by category\n{chart}{dashed_line}\n{names}"

please provide all your code and a link to the project

Please post all your code.

class Category:
    def __init__(self, name):
        self.name = name
        self.ledger = []
    
    def __str__(self):
        items = '\n'.join([f"{item['description'][:23]:<23}{item['amount']:>7.2f}" for item in self.ledger])
        return f"{self.name:*^30}\n{items}\nTotal: {self.get_balance():.2f}"

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

    def withdraw(self, amount, description=""):
        if self.check_funds(amount):
            self.ledger.append(
                {
                    'amount': -amount,
                    'description': description
                }
            )
            return True
        return False
    
    def get_balance(self):
        return sum(item['amount'] for item in self.ledger)
    
    def transfer(self, amount, other_category):
        if self.check_funds(amount):
            self.withdraw(amount, f"Transfer to {other_category.name}")
            other_category.deposit(amount, f"Transfer from {self.name}")
            return True
        return False
    
    def check_funds(self, amount):
        return self.get_balance() >= amount

def create_spend_chart(categories):
    spends = [sum(-item['amount'] for item in category.ledger if item['amount'] < 0) for category in categories]
    total_spent = sum(spends)
    percentages = [int(spend / total_spent * 10) * 10 for spend in spends]
    longest_name_length = max(len(category.name) for category in categories)
    chart = ""
    for i in range(100, -1, -10):
        line = f"{i:>3d}|"
        content = ''.join(" o " if percent >= i else "   " for percent in percentages)
        chart += f"{line}{content} \n"
    names = ""
    dashed_line = " "*4 + "-"*3*len(categories) + "-"
    for l in range(longest_name_length):
        line = "    "
        content = ''.join(f" {category.name[l]} " if len(category.name) > l else "   " for category in categories)
        names += f"{line}{content} \n"
    return f"Percentage spent by category\n{chart}{dashed_line}\n{names}"
class Category:
    def __init__(self, name):
        self.name = name
        self.ledger = []
    
    def __str__(self):
        items = '\n'.join([f"{item['description'][:23]:<23}{item['amount']:>7.2f}" for item in self.ledger])
        return f"{self.name:*^30}\n{items}\nTotal: {self.get_balance():.2f}"

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

    def withdraw(self, amount, description=""):
        if self.check_funds(amount):
            self.ledger.append(
                {
                    'amount': -amount,
                    'description': description
                }
            )
            return True
        return False
    
    def get_balance(self):
        return sum(item['amount'] for item in self.ledger)
    
    def transfer(self, amount, other_category):
        if self.check_funds(amount):
            self.withdraw(amount, f"Transfer to {other_category.name}")
            other_category.deposit(amount, f"Transfer from {self.name}")
            return True
        return False
    
    def check_funds(self, amount):
        return self.get_balance() >= amount

def create_spend_chart(categories):
    spends = [sum(-item['amount'] for item in category.ledger if item['amount'] < 0) for category in categories]
    total_spent = sum(spends)
    percentages = [int(spend / total_spent * 10) * 10 for spend in spends]
    longest_name_length = max(len(category.name) for category in categories)
    chart = ""
    for i in range(100, -1, -10):
        line = f"{i:>3d}|"
        content = ''.join(" o " if percent >= i else "   " for percent in percentages)
        chart += f"{line}{content} \n"
    names = ""
    dashed_line = " "*4 + "-"*3*len(categories) + "-"
    for l in range(longest_name_length):
        line = "    "
        content = ''.join(f" {category.name[l]} " if len(category.name) > l else "   " for category in categories)
        names += f"{line}{content} \n"
    return f"Percentage spent by category\n{chart}{dashed_line}\n{names}"

Note: open the browser console with F12 to see a more verbose output of the tests.

Did you check the browser console output? There will be an error there with formatting information.

Yes, I even looked up what the +/- thing means and copied the result in the console lo compare it and I can’t find the error, I feel dumb.

look at the assertion error, there is more visible
or maybe written like this?

AssertionError: 'Perc[364 chars]         m  \n           e  \n           n  \n           t  \n' 
             != 'Perc[364 chars]         m  \n           e  \n           n  \n           t  '

where is the difference?

ohh there is a \n, thank you very much!