Build a Budget App - Build a Budget App

Tell us what’s happening:

create_spend_chart should have correct percentages down the left side error always occurs to me althought they are already set to the left side correctly

Your code so far

class Category:
    def __init__(self,name):
        self.name=name
        self.ledger=[]
    def deposit(self,amount, description=''):
        if amount<0:
            raise ValueError("deposited amounts must be positive!!!")
            return
        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):
        return sum(thing["amount"] for thing in self.ledger)
    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
        else:
            return False
    def check_funds(self,amount):
        if sum(something["amount"] for something in self.ledger)>=amount:
            return True
        else:
            return False
    def __str__(self):
        title = ''
        title += self.name.center(30, '*')
        res = ''
        res += title + '\n'
        total = 0
        for i in self.ledger:
            d = i["description"][:23]
            if not 'deposit' in res:
                if 'Transfer' in i["description"]:
                    d = i["description"]
            a = f'{i["amount"]:.2f}'
            l = f'{d}' + a.rjust(len(title) - len(d)) + '\n'
            total += i["amount"]
            res += l
        t = f'Total: {total:.2f}'
        res += t
        return res
        
def create_spend_chart(categories):
    ch="Percentage spent by category\n"
    for i in range(100,0,-10):
        ch+=f"{i:3}|"
        for category in categories:
            s=0
            for thing in category.ledger:
                if thing['amount']<0:
                    s+=abs(thing["amount"])
            p=round(s*100/category.get_balance(),-1)
            if p>=i:
                ch+=' o '
            else:
                ch+='   '
        ch+='\n'
    ch+='    '+'-'*(3*len(categories)+1)
    max_len=max(len(category.name) for category in categories) 
    ch+='\n'
    for r in range(max_len):
        ch+='    '
        for category in categories:
            if len(category.name)-1>=r:
                ch+=' '+category.name[r]+' '
            else:
                ch+='   '
        if r!=max_len-1:
            ch+='\n'
    return ch
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(food)
print(clothing)
print(create_spend_chart([food,clothing]))

Your browser information:

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

Challenge Information:

Build a Budget App - Build a Budget App

GitHub Link: https://github.com/freeCodeCamp/freeCodeCamp/blob/main/curriculum/challenges/english/blocks/lab-budget-app/5e44413e903586ffb414c94e.md

Welcome to the forum @melek2006.mw

Shouldn’t the percentages start at zero?

Happy coding

Please check this. We need to include 0%, but range(100, 0, -10) stops at 10. It should be changed so that 0 is included as well.