Build a Budget App - Build a Budget App

Tell us what’s happening:

test19, to test 24 fail. I think the code is right because of the output in the terminal so I don’t know where are the mistakes I am making. Please help. Thanks in advance

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):
        sum = 0
        for index in range(0, len(self.ledger)):
            sum += self.ledger[index]['amount']
        return sum

    def transfer(self, amount, destination):
        source = self
        if source.check_funds(amount):
            source.withdraw(amount,f'Transfer to {destination.name}')
            destination.deposit(amount,f'Transfer from {source.name}' )
            return True
        else:
            return False
    def check_funds(self, amount):
        try:
            if self.get_balance() >= amount:
                return True
            return False
        except:
            return False
    def __str__(self):
        star = '*'
        result = ''
        number_of_star = 30 - len(self.name)
        if number_of_star % 2 == 0:
            result += (star * (number_of_star // 2))+self.name+(star * (number_of_star // 2))+ '\n'
        else:
            result += (star * (number_of_star // 2))+self.name+(star * ((number_of_star // 2)+1))+'\n'
        for index in range(0, len(self.ledger)):
            number = float(self.ledger[index]['amount'])
            string_number = f"{number:.2f}"
            result += f"{self.ledger[index]['description'][0:23]:23}{string_number: >7}\n"
        
        final_line = f"Total: {self.get_balance()}"

        return result + final_line
        

    
    

def create_spend_chart(categories):
    result =''
    if len(categories) >0 and len(categories)<= 4:
        percentages = []
        percentage = 1
        total = 0
        partial_total = []
        for i in range(0,len(categories)):
            partial_total.append(0)
            for j in range(0, len(categories[i].ledger)):
                if categories[i].ledger[j]['amount'] < 0:
                    partial_total[i] += categories[i].ledger[j]['amount']
                    partial_total[i]*= -1
        
            total += partial_total[i]
            percentage = (partial_total[i]/ total)*100
            percentages.append(int(percentage//10)*10)
        result += 'Percentage spent by category\n'
        balls = []
        number=1
        for i in range(0,11):
            number = i*10
            balls.append([f'{number:3}|'])
            for j in range(0, len(categories)):
                if percentages[j] >= i*10:
                    balls[i].append('o')
        for i in range(10,-1,-1):
            result += '  '.join(balls[i])
            result += '  \n'
        bar = ('---' * len(categories)) + '--\n'
        result += '    ' + bar
        names = []
        maximum = 0
        for i in range(0, len(categories)):
            if len(categories[i].name) >= maximum:
                maximum = len(categories[i].name)
        
        for j in range(0, maximum):
            names.append(['    '])
            for i in range(0,len(categories)):
                if len(categories[i].name) > j:
                    names[j].append(categories[i].name[j])
                else:
                    names[j].append(' ')
        for j in range(0, maximum-1):
            result += '  '.join(names[j]) + '  \n'
        result += '  '.join(names[maximum-1])+'  '
    return result
        
           
    

food = Category('Food')
food.deposit(1000, 'deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
print(food)
cat_list=[food, clothing]
print(create_spend_chart(cat_list))

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0

Challenge Information:

Build a Budget App - Build a Budget App

there is something not working, does not matter how many withdrawals I add, the clotjhing bar doesn’t grow and stays at 0

The tests should tell you what to do.

It does’t stay the same:

I tried with these instructions:

food = Category('Food')
food.deposit(1000, 'deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
print(food)
clothing.deposit(1000, 'deposit')
clothing.withdraw(10,'no')
clothing.withdraw(10, 'no')
clothing.withdraw(10, 'no')
clothing.withdraw(20, 'no')
clothing.deposit(1000, 'deposit')
clothing.withdraw(100, 'no')
cat_list=[food, clothing]
print(create_spend_chart(cat_list))

and in the terminal I have this output:

why is food 100%? it’s impossible you spent 160%

please read again how to calculate the percentages, if you need more infos ask about that