Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

I can’t solve the final bit the output looks the same as the expected output (for the barchart)

  • create_spend_chart should print a different chart representation. Check that all spacing is exact.

Your code so far


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36

Challenge Information:

Build a Budget App Project - Build a Budget App Project

Please post your code

Check the devtools Console for a more verbose error output, just like we did in the previous step.

is there a way to edit the post so i can paste the code in

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

    def __str__(self):
        length=len(str(self.name))
        start=(30-length)//2
        header= '*'*start+str(self.name)+'*'*start
        finals=""
        count=0
        for record in self.ledger:
            
            desc=record['description']
            if len(desc)==23:
                desc=record['description']
            elif len(desc)>23:
                desc=record['description'][:23]
            else:
                desc=desc+' '*(23-len(desc))
            
            amts=(record['amount'])
            
            if type(record['amount']) is float:
                amt=(f" {amts}")
            else:
                amt=(f" {amts}.00")
            
            if len(amt)==7:
                amt=amt
            elif len(amt)>7:
                amt=amt[len(amt)-7:]
            else:
                amt=' '*(7-len(record['description']))+str(amt)
            
            spacing=30-len(amt)-len(desc)
        
            finals+= "\n"+ desc +" "*(spacing)+ amt

        fund=str(self.get_balance())
        if type(self.get_balance()) is float:
            total=(f"{fund}")
        else:
            total=(f"{fund}.00")
        total="\nTotal: " + total
            
        return header + finals + total



    def deposit(self,amount,description=''):
        newEntry={"amount": amount, "description": description}
        self.ledger.append(newEntry)

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

    def withdraw(self,amount,description=''):
        withdrawal=False
        newEntry={"amount": -amount, "description": description}
        if self.check_funds(amount):
            self.ledger.append(newEntry)
            return True

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

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

    
def create_spend_chart(categories):
    barchart=""
    barchartL=[]
    percents=[]
    totalSpent=0
    spending=[]
    count=100
    while count>=0:
        if count==100:
            barchartL.append(str(count)+"|")
        elif count!=0:
            barchartL.append(" "+str(count)+"|")
        else:
            barchartL.append("  "+str(count)+"|")
        count-=10

    barchartL.append(' '*4+'-'*(3*len(categories)+1))
    
    for item in categories:
        spent=0
        for amount in item.ledger:
            if amount['amount']<0:
                spent+=amount['amount']
        spending.append(-spent)
    for spent in spending:
        totalSpent+=spent

    for index,item in enumerate(categories):
        percent=round(spending[index]/totalSpent*100,-1)
        percents.append(percent)

    for index,per in enumerate(percents):
        per+=10
        while per>0:
            barchartL[11-int(per/10)]+= ' '*((1+index)*3+1-len(barchartL[11-int(per/10)]))+' o'
            per-=10
        
    categoriesL=[]
    for item in (categories):
        categoriesL.append(item.name)
    
    items=0
    categoriesL.sort(reverse=True)
    for char in categoriesL[0]:
        barchartL.append(' ')

    while items<len(categoriesL):
        expectedLength=5+items*3

        for l in range(len(categoriesL[0])):
            barchartL.append(' '*3)
            
        count=12
        for char in categories[items].name:
            barchartL[count]+=' '*(expectedLength-len(barchartL[count]))+ char 
            count+=1

        items+=1

    for line in barchartL:
        barchart+=line +'\n'
    
    barchart=barchart.strip()

    return ("Percentage spent by category" + "\n"+barchart)

#example
food = Category("Food")
entertainment = Category("Entertainment")
business = Category("Business")    
food.deposit(900, "deposit")
entertainment.deposit(900, "deposit")
business.deposit(900, "deposit")
food.withdraw(105.55)
entertainment.withdraw(33.40)
business.withdraw(10.99)

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

i tried but i cant figureo ut what the problem is
: Expected different chart representation. Check that all spacing is exact.
pyodide.asm.js:9 AssertionError: ‘Perc[25 chars]n100|\n 90|\n 80|\n 70| o\n 60| o\n 50| [262 chars] t’ != 'Perc[25 chars]n100| \n 90| \n 80| [349 chars] t ’

but i cant figure out how it shld be altered/what is wrong

let’s look at it with the spaces

AssertionError: 'Perc[25 chars]n100|\n 90|\n 80|\n 70|    o\n 60|    o\n 50| [262 chars]   t' != 
                'Perc[25 chars]n100|          \n 90|          \n 80|         [349 chars] t  '

the one above is yours, the one below is the expected.
The graph should be a rectangle of characthers, the spaces should not be missing

how do i figure out how many spaces to add

i tried that but it doesnt work
barchart+=line +" "*(15-len(line))+‘\n’

im guessing i did teh wrong number of spaces but how many should i need

enough to fill the line, remember that there can be up to four categories, it depends on how many categories you have

barchart+=line+" "(3len(categoriesL)-len(line))+ ‘\n’
i tried to account for that here

AssertionError: ‘Perc[30 chars] \n 90| \n 80| \n 70| o\n 60| [307 chars] \n’ != 'Perc[30 chars] \n 90| \n 80| \n 7[344 chars] t ’

i noticed i shouldve probably included the 4 characters before in 100| etcbut
barchart+=line+" "(4+3 len(categoriesL)-len(line))+ ‘\n’ doesn’t work etierh

'Perc[34 chars]     \n 90|         \n 80|         \n 70|    o[363 chars]  \n' != 
'Perc[34 chars]      \n 90|          \n 80|          \n 70|  [340 chars] t  '

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Getting there, you still need one more space

do you mean changing it to barchart+=line+" "*(∫5∫+3* len(categoriesL)-len(line))+ ‘\n’

cuz i didnt manage to get that to work either

i tried adding one but it didnt work-- unless im misunderstanding where to add it

Your output has 5 spaces here. The expected output has 6 spaces, that’s why they don’t line up.

Please share the new error if you’ve updated your code.

Perc[170 chars] 10| o o o \n 0| o o o \n ----------[254 chars] \n
'Perc[170 chars] 10| o o \n 0| o o o \n ----------[204 chars] t ’

i made it have the extra space barchart+= line+ " "(5+3len(categoriesL)-len(line))+ “\n”

Enclose this in triple backticks, so we can see it in fixed width code. It’s easier to see the spacing this way.