Scientific Computing with Python Projects - Budget App

Tell us what’s happening:

For the last requirement, I can not understand what is wrong with the output.

Your code so far

class Category:
    amount = 0
    name = ''
    ledger = tuple()
    def __init__(self, name):
        self.name = name
    
    def __str__(self):
        budget = 0
        name = f"{self.name:*^30}"
        printing = ''
        for i in self.ledger:
            descr = i["description"][:23]
            budget += i["amount"]
            budget_str = str(i["amount"])
            if len(budget_str) > 7:
                budget_str = budget_str[0:7] 
                printing = printing+'\n'+'{:23s}{:7s}'.format(descr, budget_str)
            else:
                printing = printing +'\n'+'{:23s}{:7.2f}'.format(descr, float(budget_str))
        message = name + printing +'\n'+ str('Total: %.2f' %budget)
        return message

    def deposit(self, amount, description = None):
        self.amount += amount
        self.ledger = self.ledger + ({"amount" : amount, "description" : description if description != None else ''}, )

    def withdraw(self, amount, description=None):
        if self.check_funds(amount) == True:
            self.amount -= amount
            self.ledger = self.ledger + ({"amount" : -amount, "description" : description if description != None else '' }, )

            return True
        else:
            return False
        

    def get_balance(self):
        return self.amount

    def transfer(self, amount, budget):
        if self.check_funds(amount) == True:
            budget.deposit(amount, "Transfer from " + str(self.name))
            self.withdraw(amount, "Transfer to " + str(budget.name) )
            return True
        else:
            return False

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

def create_spend_chart(categories):
    percent = len(categories)*[0]
    indice = 0
    expense_total = 0
    each_expens = len(categories)*[0]
    for inst in categories :
        for expens in inst.ledger:
            if expens["amount"] < 0:
                expense_total += expens["amount"]
                each_expens[indice] += expens["amount"]
        indice +=1

    expense_total = -expense_total

    for indice in range(len(categories)) :
        if -expense_total != 0 :
            percent[indice] = int(((-each_expens[indice] * 100/ expense_total) // 10) * 10)
        else :
            percent[indice] = 0
        indice += 1

    spend_chart = 'Percentage spent by category'
    for p in range(100, -10, -10) :
        line = '{:3d}{:1s}'.format(p, '|')
        for pp in percent :
            if p <= pp:
                line = line + ' ' + 'o' + ' '
            else :
                line = line + 2*' ' + ' '
        spend_chart = spend_chart + '\n' + line + 2*' '
    spend_chart = spend_chart + '\n' + 4*' '
    for barre in range(len(categories)) :
        spend_chart = spend_chart + 2*'-'
    spend_chart = spend_chart + int(len(categories))*'-'+'-'

    max_len = 0
    for inst in categories :
        if max_len < len(inst.name) :
            max_len = len(inst.name)
    for iter_maxlen in range(max_len) :
        spend_chart = spend_chart + '\n' + 4*' '
        for inst in categories :
            if len(inst.name) < (iter_maxlen + 1) :
                spend_chart = spend_chart + 2*' '+' '
                continue
            spend_chart = spend_chart + ' ' + inst.name[iter_maxlen] + ' '
        spend_chart = spend_chart + 2*' ' 

    return spend_chart

food = Category("Food")
food.deposit(1000, "deposit")
food.withdraw(10, "groceries")
food.withdraw(15, "restaurant and more food for dessert")
clothing = Category("Clothing")
clothing.deposit(50, "deposit")
clothing.withdraw(10, "pant")
auto = Category("Auto")
auto.deposit(200, "deposit")
auto.withdraw(5, "tire")
house= Category("House")
house.deposit(500, "deposit")
house.withdraw(10, "roof")


print(food)
print(clothing)
print(auto)
print(house)
print(create_spend_chart([food, clothing, auto, house]))

Your browser information:

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

Challenge Information:

Scientific Computing with Python Projects - Budget App

press F12 and run the tests, you will have a more complete output of the tests in the browser console.

'Perc[35 chars]      \n 90|           \n 80|           \n 70|[363 chars]t   '
'Perc[35 chars]     \n 90|          \n 80|          \n 70|   [339 chars] t  '

the first line is yours, the second one is the expected. It looks like you have some extra spaces.