Build a Budget App - test 19 , height of bars should be rounded down to nearest 10

Tell us what’s happening:

i cant pass the checker for the height for each bar should be rounded down to the nearest 10, that is exactly what i think my math does but it doesnt seem to be correct. i also passed all other tests. has anyone met with this problem before? thanks

Your code so far

from math import floor

class Category:
    spend = 0

    def __init__(self, name):
        self.name = name
        self.ledger =[]
        self.balance = 0

    def deposit(self, amount, description=''):
        new_deposit = {
            'amount': amount,
            'description': description
        }
        self.ledger.append(new_deposit)
        #self.balance += amount

    def withdraw(self, amount, description=''):
        if not self.check_funds(amount): return False
        new_withdrawal = {
            'amount': -amount,
            'description': description
        }
        self.ledger.append(new_withdrawal)
        #self.balance-=amount
        return True
    
    def get_balance(self):
        total =0
        for transaction in self.ledger:
            total+=transaction['amount']
        return total
    
    def transfer(self, amount, other):
        if not self.check_funds(amount): return False
        self.withdraw(amount, f'Transfer to {other.name}')
        other.deposit(amount, f'Transfer from {self.name}')
        return True
    
    def check_funds(self, amount):
        if amount>self.get_balance(): return False
        return True

    def __str__(self):
        title = (f"{self.name:*^30}\n")
        menu = ""
        for transaction in self.ledger:
            menu+=(f"{transaction['description'][:23]:23}{transaction['amount']:7.2f}\n")
        total = f"Total: {self.get_balance()}"
        return (title + menu + total)


def create_spend_chart(categories):
    for ctg in categories:
        ctg.spend = 0
        for item in ctg.ledger:
            if item['amount'] < 0:
                ctg.spend += -item['amount']
        Category.spend += ctg.spend
        # print(ctg.name, ctg.spend, Category.spend)
    
    
    title = ('Percentage spent by category')
    chart = ""
    for cent in range(100,-1,-10):
        chart += (f'\n{cent:3d}| ')
        for ctg in categories:
            ctg.percent = 100*ctg.spend/Category.spend
            rounded = (ctg.percent//10)*10
            if rounded >= cent:
                chart += 'o  '
            else: chart+='   '

    part = '    '
    chart += f'\n{part:-<{len(categories)*3+5}}'
    vertical = ''
    names = [category.name for category in categories]
    longest = max(len(name) for name in names)

    for i in range(0,longest):
        vertical += f'\n{part} '
        for ctg in categories:
            if i <= len(ctg.name)-1:
                vertical += f'{ctg.name[i]}  '
            else : vertical += '   '

    return (f'{title}{chart}{vertical}')

def main():
    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')
    auto = Category('Auto')
    auto.deposit(500)
    # auto.withdraw(48)
    food.transfer(50, clothing)
    # clothing.withdraw(45)
    # print(food)
    print(create_spend_chart([food, clothing, auto]))
    print(floor(food.percent), floor(auto.percent), floor(clothing.percent), (5//10))

if __name__ == '__main__':
    main()

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.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

if I try with

print(create_spend_chart([clothing, auto]))

the two bars are now 20% and 0%

if auto has no expenses, it should be 100% and 0%

you need to calculate the percentage expenses based on the total expenses of the list of categories

using Category.spend make so that you can’t do that

but the attr Category.spend is only first created when the chart is called, making it take only [clothing, auto] as the only two categories in the list in its calculation .

try to use create_spend_chart twice in a row

ok got it, the second one is not adding up to 100.. will update now and see what happens, thanks.

i set Category.spend to 0 at the first line ofcreate_spend_chart so it restarts the calculation every time insted of making a new independent variable. it worked thanks alot.

but im wondering if that would be technically correct? because im putting the value relating to only some of the category objects and assigning it as an attribute for the class itself.

you should not use Category.spend, your total has no reason to exist outisde create_spend_chart