Build a Budget App - Build a Budget App Test 17

Tell us what’s happening:

Hi, I’m trying to complete test 17, I wanted to revisit it later after completing the rest of the code, but it’s really bothering me that it isn’t completed. If I place ‘return header’ on line 59, it completes the test, but if I place it on or after line 64, it starts failing again, and I, for the life of me, cannot figure out where it is going wrong, nor can I fix the code without something else going wrong. I feel like there’s something simple I’m just missing out on.

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
        else:
            return False

    def get_balance(self):
        current_balance = 0
        for transaction in self.ledger:
            current_balance += transaction['amount']
        return current_balance
            

    def transfer(self, amount, destination):
        if self.check_funds(amount):
            prior_bal = destination.get_balance()

            self.withdraw(amount, f'Transfer to {destination.name}')
            destination.deposit(amount, f'Transfer from {self.name}')

            if prior_bal != destination.get_balance():
                return True
        else:
            return False

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

    def __str__(self):
        header = self.name.center(30, '*') + '\n'
        items = ''
        for trans in self.ledger:
            desc = trans['description'][:23]
            amnt = '{:.2f}'.format(trans['amount'])[:7]
            space = (" " * (30 - len(desc) - len(amnt)))
            full_line = desc + space + amnt + '\n'
            items += full_line
        items += f'Total: {self.get_balance()}'
        return header + items

def create_spend_chart(categories):
    header = 'Percentage spent by category'
    chart = ''
    withdrawl_list = []
    total_withdrawn = 0
    chart_percents = []
    
    for trans in categories.ledger:
        if trans['amount'] < 0:
            total_withdrawn += abs(trans['amount'])
            withdrawl_list.append(trans)
    
    for withdrawl in withdrawl_list:
        percent = 10 * round((withdrawl['amount'] / total_withdrawn) * 100/ 10)
        chart_percents.append(abs(percent))


    chart += '\n'
    for i in range(100, -1, -10):
        chart += f'{i:3}| '

        for percent in chart_percents:
            if percent >= i:
                chart += 'o  '
            else:
                chart += '   '

        chart += '\n'

    chart += "    " + (len(chart_percents) * 3 + 1) * "-"
    

    


            
    
    
    10 * round(trans['amount'] / 10) 

    return header + chart


test1 = Category('test1')
test2 = Category('test2')

test1.deposit(10000, 'Inital Deposit')
test2.deposit(2000)
test1.transfer(1000, test2)
test1.withdraw(800, "withdrawl")
test1.withdraw(2000, "withdrawl2")
test1.withdraw(1, "withdrawl3")

printtest = Category('printtest')

printtest.deposit(1000, "Inital Deposit")
printtest.withdraw(20, "Lengthy withdrawl test. You should not see this sentence.")

print(test1)
print(create_spend_chart(test1))

Your browser information:

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

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

is that called with a list of categories? once you call it correctly, you will see the error that is failing all tests

I noticed the instructions do not mention it, so I have opened a ticket to update to

You should have a function outside the Category class named create_spend_chart(categories) that takes a list of categories and returns a bar-chart string. To build the chart:

Oh, so it’s that create_spend_chart should be able to take in several of the class arguments then? (Or rather, several in a list-) I was under the impression that it was meant to deal with the withdrawals in a single Category class, though I guess that’s mostly down to me not putting context clues together. Thanks for your help!