Build a Budget App - Build a Budget App

Tell us what’s happening:

i was doing the “build a budget app” and i finished the graph and it have the same format as the given output however the site doesn’t consider it as done why even the test 17 which is just printing the title isn’t consider as done

Your code so far

class Category:
    def __init__(self,name):
        self.name =name
        self.ledger =[]
        self.balence=0
        self.spent =0
    def deposit(self,amount,description=""):
        self.ledger.append({'amount': amount, 'description': description})

    def withdraw(self,amount,description=""):
        if self.get_balance() >= amount:
            self.ledger.append({'amount': -amount, 'description': description})
            return True
        else:
            return False
    def get_balance(self):
        bal=0
        for leg in self.ledger:
            for val in leg.values():
                if isinstance(val,int)or isinstance(val,float):
                    bal+=val
                    self.balence = bal
        return bal

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

    def check_funds(self, amount):
        return True if self.get_balance() >= amount else False
    
    def __str__(self):
        title = f"{self.name:*^30}\n"
        items = ''
        for item in self.ledger:
            description = item['description'][:23]
            amount = f"{item['amount']:.2f}"
            items +=f"{description:<23}{amount:>7}\n"
        total = f"Total: {self.get_balance():.2f}"
        return f"{title}{items}{total}"

    def total_spent(self):
        sp = 0
        for item in self.ledger:
            amount = item['amount']
            if amount < 0:
                sp += -amount
        self.spent = sp
        return sp

def create_spend_chart(categories):
    title = "\nPercentage spent by category"
    total =0
    for categorie in categories:
        total += categorie.total_spent()

    percent ={}
    for categorie in categories:
        x =round(categorie.total_spent()/total*100)
        x = rounder(x)
        percent.update({""+categorie.name: x})
    print(title)
    for i in reversed(range(11)):
        dot = "o"
        x =""
        for categorie in categories:
            if percent[categorie.name] > (i-1)*10:
                x+= dot+"  "
        if i == 10:
            print(f"{i*10}| {x}")
        elif i ==0:
            print(f"  0| {x}")
        else:
            print(f" {i*10}| {x}")
    print("    ----------")
    lon= longer(categories)
    for i in range(lon):
        y = "     "
        for categorie in categories:
            try:
                y += categorie.name[i]+"  "
            except IndexError:
                y+= "   "
        print(y)
        
def longer(categories):
    ma= -1
    for categorie in categories:
        if ma < len(categorie.name):
            ma = len(categorie.name)
    return ma
        


def rounder(x):
    return x//10*10

    
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')
clothing.deposit(1000, 'initial deposit')
clothing.withdraw(200, 'd')
food.transfer(50, clothing)
print(food)

create_spend_chart([clothing,food])

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 OPR/133.0.0.0

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

are you sure this is how it should start?

even if i remove it and start he title directly the problem is not resolved and test 17 is still x (like the rest from 18 till the end).

remember the difference between printing and returning

   def create_spend_chart(categories):
    chart=""
    chart+="Percentage spent by category"
    chart+="\n"
    total =0
    for categorie in categories:
        total += categorie.total_spent()

    percent ={}
    for categorie in categories:
        x =round(categorie.total_spent()/total*100)
        x = rounder(x)
        percent.update({""+categorie.name: x})

        
    for i in reversed(range(11)):
        dot = "o"
        x =""
        for categorie in categories:
           if percent[categorie.name] > (i-1)*10:
                x+= dot+"  "
        x+= " "*((len(categories)*3)-len(x))
        if i == 10:
            chart+=f"{i*10}| {x}\n"
        elif i ==0:
            chart+=f"  0| {x}\n"
        else:
            chart+=f" {i*10}| {x}\n"
        sp="-"
        s=" "
    chart+=f"    {sp*len(categories)*3} \n"

    lon= longer(categories)
    for i in range(lon):
        y = "     "
        for categorie in categories:
            try:
                y += categorie.name[i]+"  "
            except IndexError:
                y+= "   "
        chart+=f"{y}\n"

    return f"{chart}"
        
def longer(categories):
    ma= -1
    for categorie in categories:
        if ma < len(categorie.name):
            ma = len(categorie.name)
    return ma
        


def rounder(x):
    return x//10*10

i update it now the 17 18 20 22 and 23 work but i dont get why the rest dont and especially the 19

check with printing both

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

the bars should not stay the same when the categories swap position, right?

ow nice one thank for the remake! and i found the problem

if percent[categorie.name] > (i-1)*10:
                x+= dot+"  "
            else:
                x+= "   "

there wasn’t an else function the when the percent was lower nothing will be added leading to this problem

and i had a small problem with the line of “-” noe its work thank youu!

look at your dashes vs the example, you do not have the right number of dashes

also careful with spaces, make sure to have them only where needed