Build a Budget App - Build a Budget App

Tell us what’s happening:

Can someone help me understand why test 16 is not passing? my code seems to be working correctly in terms of printing different categories but the test keeps failing.

Your code so far

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

    def get_balance(self):
        amounts = [i['amount'] for i in self.ledger]
        return sum(amounts)

    def check_funds(self, amount):
        balance = self.get_balance()
        if amount > balance:
            return False
        return True

    def deposit(self, amount, desc = ""):
        self.ledger.append({
            'amount': amount,
            'description': desc
        })
        # return self.get_balance()
    
    def withdraw(self, amount, desc = ""):
        if self.check_funds(amount):
            self.ledger.append({
                'amount': -amount,
                'description': desc
            })
            return True
        else:
            return False
    def transfer(self, amount, to_cat):
        if self.check_funds(amount):
            self.withdraw(amount, desc = f"Transfer to {to_cat.name}")
            to_cat.deposit(amount, desc = f"Transfer from {self.name}")
            return True
        else:
            return False
    
    def __str__(self):
        # name_length = len(self.name)
        # num_stars = 30 - name_length
        details = ''
        for entry in self.ledger:
            amount = "{:.2f}".format(round(float(entry['amount']), 2))
            desc = entry['description'][:23]
            details += f'{desc.ljust(23)} {str(amount).rjust(7)}'
            details += '\n'
        details += 'Total: {:.2f}'.format(self.get_balance())

        return (
            self.name.center(30, '*') + '\n' + details
        )

def create_spend_chart(categories):
    title = 'Percentage spent by category'
    # percentages = {}
    withdrawals = {}
    total_spent = 0
    for cat in categories:
        entries = []
        for entry in cat.ledger:
            if entry['amount'] < 0:
                entries.append(abs(entry['amount']))
                withdrawals[cat.name] = entries
    for w in withdrawals.values():
        total_spent += sum(w)
    percentages = {k: int(round(sum(v), -1)) for k, v in withdrawals.items()}
    

food = Category('Food')
food.deposit(100, desc = 'initial deposit')
food.withdraw(10.15, 'cash report')
books = Category('books')
food.transfer(23, books)
books.withdraw(2.34)
# print(food.get_balance())
# print(food.ledger)
print(food)
create_spend_chart([food, books])

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.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

Hi @albasfaisal,

What is create_spend_chart returning?

Happy coding

currently, nothing, i haven’t completed it yet, i’ve only calculate the percentages

Try testing with the example usage in the instructions (User Story #4) and compare the spacing produced by your code to what is showing in the instructions:

*************Food*************
initial deposit        1000.00
groceries               -10.15
restaurant and more foo -15.89
Transfer to Clothing    -50.00
Total: 923.96