Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

I couldn’t find anything wrong with the create spend chart(categories) code, but the test didn’t work even though the results matched the example

Your code so far

class Category:
    def __init__(self, category):
        self.category = category
        self.ledger = []

    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, category_name):
        if self.check_funds(amount):
            self.withdraw(amount, f'Transfer to {category_name.category}')
            category_name.deposit(amount, f'Transfer from {self.category}')
            return True
        return False

    def check_funds(self, amount):
        return self.get_balance() >= amount

    def __str__(self):
        title = self.category.center(30, "*") + "\n"
        items = ""
        for item in self.ledger:
            items += f"{item['description'][:23]:23}{item['amount']:>7.2f}\n"
        total = f"Total: {self.get_balance():.2f}"
        return title + items + total

def create_spend_chart(categories):
    total_withdrawn = sum(sum(item["amount"] for item in cat.ledger if item["amount"] < 0) for cat in categories)
    
    percentages = [(sum(item["amount"] for item in cat.ledger if item["amount"] < 0) / total_withdrawn) * 100 for cat in categories]
    
    chart = "\nPercentage spent by category\n"
    for i in range(100, -1, -10):
        chart += f"{i:>3}| "
        for percent in percentages:
            chart += 'o' if (percent // 10) * 10 >= i else " "
            chart += "  "
        chart += "\n"
    chart += "    " + "-" * (len(categories) * 3 + 1) + '\n'
    max_name_length = max(len(cat.category) for cat in categories)
    for i in range(max_name_length):
        chart += "     "
        for cat in categories:
            chart += cat.category[i] if i < len(cat.category) else " "
            chart += "  "
        chart += "\n"
    
    print(chart)

categories = [
    Category('Food'),
    Category('Clothing'),
    Category('Auto')
]
categories[0].deposit(1000, 'deposit')
categories[1].deposit(1000, 'deposit')
categories[2].deposit(1000, 'deposit')
categories[0].withdraw(80.15, 'groceries')
categories[2].withdraw(50.15, 'groceries')
categories[1].withdraw(100, 'gorgious')
categories[0].withdraw(15.89, 'restaurant and more food for dessert')
categories[0].transfer(150, categories[1])

create_spend_chart(categories)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36

Challenge Information:

Build a Budget App Project - Build a Budget App Project

First of all, you need to return the chart, not print it. Then press F12 to open the console. You’ll see the difference between your output and the expected one.

Thanks for your advice