Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

Hi freecodecamp staff or anyone, I’ve spent 6 hours on this, still got stuck on this, my result seems to be the same as per the requirement, but the console doesn’t pass my code. Could you tell me where went wrong please? Thank you!

Your code so far

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

    def __str__(self):
        title = f"{self.category:*^30}\n"
        items =""
        for entry in self.ledger:
            items += f"{entry['description'][:23]:23}{entry['amount']:7.2f}\n"
        total = f"Total: {self.get_balance():.2f}"
        return title + items + total
      
    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(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.category}")
            other_category.deposit(amount, f"Transfer from {self.category}")
            return True
        return False
        
    def check_funds(self, amount):
        return self.get_balance() >= amount

    def get_withdrawals(self):
        return sum(-entry["amount"] for entry in self.ledger if entry["amount"] < 0)


def create_spend_chart(categories):
    if len(categories) > 4:
        raise ValueError
    
    total_spent = sum(category.get_withdrawals() for category in categories)
    percentages = [round((category.get_withdrawals() / total_spent) * 100, -1) for category in categories]

    chart = "Percentage spent by category\n"
    for i in range(100,-1, -10):
        chart += f"{i:3}| "
        for percentage in percentages:
            chart += "o  " if percentage >= i else "   "
        chart += "\n"

    chart += "    -" + "---" * len(categories) + "\n"

    max_name_length = max(len(category.category) for category in categories)
    names = [category.category.ljust(max_name_length) for category in categories]
    for i in range(max_name_length):
        chart += "     "
        for name in names:
            chart += name[i] + "  "
        chart += "\n"

    return chart.rstrip("\n")


food = Category("Food")
food.deposit(1000, "deposit")
food.withdraw(30, "groceries")
food.withdraw(10, "restaurant and more food for dessert")

clothing = Category("Clothing")
auto = Category("Auto")
food.transfer(50, clothing)
clothing.deposit(500, "deposit")
clothing.withdraw(100, "clothes")
auto.deposit(1000, "car deposit")
auto.withdraw(100, "repair")
entertainment = Category("Entertainment")
entertainment.deposit(500,"deposit")
entertainment.withdraw(100.10)

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36

Challenge Information:

Build a Budget App Project - Build a Budget App Project

If you check the browser’s console (F12), output of the test there shows something might not be right with rounding the values on the chart.

1 Like

something indeed went wrong! thanks mate