Build a Budget App - Build a Budget App

Tell us what’s happening:

looking for some help with Build a Budget App

Your code so far

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):
        balance = 0
        for item in self.ledger:
            balance += item['amount']
        return 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:
            desc = item['description'][:23]
            amt = f"{item['amount']:.2f}"[:7]
            items += f'{desc:<23}{amt:>7}\n'
        
        total = f'Total: {self.get_balance():.2f}'
        return title + items + total
    
def create_spend_chart(categories):
    total_withdrawals = 0
    for category in categories:
        for item in category.ledger:
            if item["amount"] < 0:
                total_withdrawals += abs(item["amount"])
    percentages = []
    for category in categories:
        spent = 0
        for item in category.ledger:
            if item["amount"] < 0:
                spent += abs(item["amount"])
        percentage = (spent / total_withdrawals) * 100
        percentages.append(int(percentage // 10) * 10)
    chart = "Percentage spent by category\n"
    for i in range(100, -1, -10):
        chart += str(i).rjust(3) + "| "
        for percent in percentages:
            if percent >= i:
                chart += "o  "
            else:
                chart += "   "
        chart += "\n"
    chart += "   " + "-" * (len(categories) * 3 + 1) + "\n"
    max_name_length = max(len(category.name) for category in categories)
    for i in range(max_range_length):
        chart += "   "
        for category in categories:
            if i < len(category.name):
                chart += category.name[i] + "  "
            else:
                chart += "   "
        if category != categories[-1]:
            chart += "\n" if i < max_name_length -1 else""
        else:
            chart += "" if i ==max_name_length -1 else "\n"
    return chart


        
            

    


Your browser information:

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

Challenge Information:

Build a Budget App - Build a Budget App

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-budget-app/5e44413e903586ffb414c94e.md at main · freeCodeCamp/freeCodeCamp · GitHub

what kind of help are you looking for?

well the reason it does’nt pass its beacause test 17 of build a budget want “Percentage spent by category” which i wrote correctly but is still does not pass i dont if it is the spaces or what is the problem

have you tried calling the function?

like

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)

print(create_spend_chart([food, clothing]))

with this below your code, there is an error in the terminal that needs addressing