Build a Budget App Project

I can’t make this error pass: create_spend_chart should print a different chart representation. Check that all spacing is exact.

Code:

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 self.get_balance() >= amount
    
    def __str__(self):
        title_line = f"{self.name:*^30}\n"
        items = ""
        for item in self.ledger:
            description = item["description"][:23]
            amount = f"{item['amount']:.2f}"
            items += f"{description:<23}{amount:>7}\n"
        total = f"Total: {self.get_balance():.2f}"
        return title_line + items + total

def create_spend_chart(categories) -> str:
    title = "Percentage spent by category\n"
    
    # Calculate the total spent across all categories
    total_spent = sum(
        sum(-item["amount"] for item in cat.ledger if item["amount"] < 0)
        for cat in categories
    )
    
    # Calculate the percentage spent per category
    spent_percentages = [
        (sum(-item["amount"] for item in cat.ledger if item["amount"] < 0) / total_spent) * 100
        for cat in categories
    ]

    # Build the percentage bars
    chart = title
    for i in range(100, -1, -10):
        chart += f"{i:>3}| "
        for percentage in spent_percentages:
            if percentage >= i:
                chart += "o  "
            else:
                chart += "   "
        chart += "\n"

    # Add the horizontal line
    chart += "    -" + "---" * len(categories) + "\n"

    # Get the maximum length of category names
    max_length = max(len(cat.name) for cat in categories)
    
    # Build the names vertically
    for i in range(max_length):
        chart += "     "
        for cat in categories:
            if i < len(cat.name):
                chart += cat.name[i] + "  "
            else:
                chart += "   "
        chart += "\n"

    return chart.rstrip()
1 Like

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.