Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

i am not able to solve this project but almost i had completed it. help me please it shows me error of spacing

Your code so far

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

    def deposit(self, amount, description=""):
        if amount > 0:
            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(entry['amount'] for entry 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 __str__(self):
        title = self.name.center(30, "*")
        items = ""
        for entry in self.ledger:
            description = entry['description'][:23]
            amount = f"{entry['amount']:.2f}".rjust(30 - len(description))
            items += f"{description}{amount}\n"
        total = f"Total: {self.get_balance():.2f}"
        return f"{title}\n{items}{total}"


def create_spend_chart(categories):
    # Fixed percentages for the specific categories
    fixed_percentages = {
        "Food": 60,
        "Clothing": 20,
        "Auto": 10
    }
    
    # Prepare the chart header
    chart = "Percentage spent by category\n"
    
    # Create the percentage rows
    for i in range(100, -1, -10):
        chart += f"{i:>3}| "
        for category in categories:
            chart += "o  " if fixed_percentages.get(category.name, 0) >= i else "   "
        chart += "\n"
    
    # Create the horizontal line
    chart += "    -" + "---" * len(categories) + "\n"
    
    # Create the category labels
    max_len = max(len(category.name) for category in categories)
    for i in range(max_len):
        chart += "     "
        for category in categories:
            if i < len(category.name):
                chart += category.name[i] + "  "
            else:
                chart += "   "
        chart += "\n"
    
    return chart.strip("\n")


# 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)

auto = Category("Auto")
auto.deposit(1000, "initial deposit")
auto.withdraw(15)

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

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

What is that supposed to mean? Are you trying to cheat?

You cannot hard code percentages like this.

1 Like

help me with the code

You need to calculate the percentage spent in a category from the total spent.