Build a Budget App Project -Step 16 printing Category

Tell us what’s happening:

Hi,
Running all tests with success except step 16. In trouble to identify why?
Looking at the output result seem to be ok.
Thanks,
Sergio

Your code so far

class Category:
    
    def __init__(self,category):
        self.ledger=[]
        self.cat=category

    def get_balance(self):
        balance=0
        for value in self.ledger:
            balance+=value["amount"]
        return balance

    def __str__(self):
  
        cat_string=self.cat.center(30,"*")
        for line in self.ledger:
            cat_string+=f"\n{line['description'][0:23].ljust(23)} "+f"{line['amount']:.2f}".rjust(7)
        cat_string+=f"\nTotal: {self.get_balance():.2f}"
        return cat_string

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

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

    def transfer(self,amount,category):
        if self.check_funds(amount):
           self.ledger.append({'amount':-amount,'description':f'Transfer to {category.cat}'})
           category.ledger.append({'amount':amount,'description':f'Transfer from {self.cat}'})
           return True
        return False   


def create_spend_chart(categories):
    max=0
    c_withdraws=[]
    total_Cwithdraws=0
    for c in categories:
        c_withdraws.append(0)
        if len(c.cat)>max:
            max=len(c.cat)
        for l in c.ledger:
            if l["amount"]<0:
                c_withdraws[len(c_withdraws)-1]+=abs(l["amount"])
        total_Cwithdraws+=c_withdraws[len(c_withdraws)-1]

    chart="Percentage spent by category"
    for n in range(100,-10,-10):
        chart+=f"\n{str(n).rjust(3)}| "
        for _ in range(len(c_withdraws)):
            if c_withdraws[_]*100/total_Cwithdraws>=n and c_withdraws[_]>0:
                chart+="o "
            else:
                chart+="  "    
            chart+=" "
    chart+="\n    "
    chart+="--".ljust(len(categories)*3,"-") 
    chart+="-"   
    for _ in range(max):
        chart+="\n    "
        for c in categories:

            if _<len(c.cat):
               chart+=f" {c.cat[_]} "
            else:
               chart+="   "   
        chart+=" "
    return chart

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)
clothing.withdraw(30,'trousses')
print(food)
print(clothing)
print("\n")
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/136.0.0.0 Safari/537.36 Edg/136.0.0.0

Challenge Information:

Build a Budget App Project - Build a Budget App Project

Welcome to the forum @sergiobgr

Compare your output to the expected output.

Happy coding

Ok, had comment the print(clothing) and even color the values like the example beside it is not requested, but still fail on 16!

Confirm that every line just have the 30 length space beside the output on the console of the exercise doesn’t align on the eye the title at the top like in the expected output.

Before this had modify the example usage on the string of the deposit method of the food instance because wasn’t matching with the expected result

So, continue to not figure out what is not ok with the string expected on 16 ?
Could you help? Thanks,
Sergio

look right above the last digit of the first number… is that an empty space?

no, to fill the space (on the eye) above the last digit of the first number only if do take 31 on method .center(31,“*”) for the string of the Category.
But still fail on testing 16

maybe consider that it’s not the first line being wrong

Well, the other lines seem to be fine. Lead there is a bug on checking this 16 item and is blocking to proceed with the course

ok, figure it out! Still with a space between description and amount in each line of ledger