Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

I cannot pass the last test

create_spend_chart should print a different chart representation. Check that all spacing is exact.

whats wrong with my code =<

Your code so far

import math

class Category:
    def __init__(self, name):
        self.name = name
        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):
        total_balance = sum(item["amount"] for item in self.ledger)
        return total_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
        return False

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

    def __str__(self):
        title = f"{self.name:*^30}\n"
        items = ""
        for item in self.ledger:
            description = item["description"][:23].ljust(23)
            amount = f"{item['amount']:.2f}".rjust(7)
            items += f"{description}{amount}\n"
        total = f"Total: {self.get_balance():.2f}"
        return title + items + total



def create_spend_chart(categories):
    total_spending = 0
    category_spend = []
    category_names = []
    category_percent = []
    graph = 'Percentage spent by category\n'

    # Get the category spending
    for category in categories:
        spending = 0
        for item in category.ledger:
            if item['amount'] < 0:
                spending += abs(item['amount']) 
        category_spend.append(spending)
        total_spending += spending
        category_names.append(category.name)

    # Get the percentage of each category
    for spend in category_spend:
        percent = (spend / total_spending) * 100
        category_percent.append(int(percent // 10) * 10)

    # Draw the graph
    for num in range(100, -10, -10):
        graph += str(num).rjust(3) + '| '
        for percent in category_percent:
            if num <= percent:
                graph += 'o  '
            else:
                graph += '   '
        graph += '\n'

    # Draw line
    graph += '    ' + '-' * (len(categories) * 3 + 1) + '\n'

    # Output categories name
    max_name_length = max(len(name) for name in category_names)
    for i in range(max_name_length):
        graph += '     '
        for name in category_names:
            if i < len(name):
                graph += name[i] + '  '
            else:
                graph += '   '
        graph += '\n'

    return graph

# Example usage:
food = Category("Food")
food.deposit(1000, "initial deposit")
food.withdraw(10.15, "groceries")
food.withdraw(15.89, "restaurant and more food for dessert")

clothing = Category("Clothing")
food.transfer(50, clothing)
clothing.withdraw(50,"ADLV")

categories = [food,clothing]
print(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/125.0.0.0 Safari/537.36

Challenge Information:

Build a Budget App Project - Build a Budget App Project

Use the console output in the browser dev tools (f12) for a more detailed error. You’ll be able to use that to troubleshoot.