Build a Budget App - Build a Budget App

Tell us what’s happening:

I have re-wrote the code twice and tried different approaches, but test 20,23,24. Please help me.
Each line in create_spend_chart chart should have the same length. Bars for different categories should be separated by two spaces, with additional two spaces after the final bar.
create_spend_chart chart should have each category name written vertically below the bar. Each line should have the same length,
create_spend_chart should print a different chart representation. Check that

Your code so far

class Category:
    def __init__(self,name):
        self.name=name
        self.ledger=[]
    
    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":round(-amount,2),"description":description})
            return True
        else: 
            return False
        
    def get_balance(self):
        balance=sum([entry["amount"] for entry in self.ledger])
        return balance

    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):
        return True if self.get_balance()>=amount else False

    def __str__(self):
        output=""
        name_len=len(self.name)
        output+=f"{'*'*((30-name_len)//2)}{self.name}{'*'*((30-name_len)//2)}"
        for entry in self.ledger:
            description=entry['description']
            amount=entry['amount']
            output+=f"\n{description[:23]}"
            output+=" "*(30-len(description[:23])-len(f"{amount:.2f}"))
            output+=f"{entry['amount']:.2f}"[:7]
        output+=f"\nTotal: {self.get_balance()}"
        return output

food = Category('Food')
food.deposit(1000, '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(50)
print(food)
shit=Category("SHIT")


def create_spend_chart(categories):
    output=""
    output+="Percentage spent by category"
    category_vise_spending_dictionary={}
    total_spending=sum([sum([entry['amount'] for entry in category.ledger if entry['amount']<=0 ])for category in categories])

    len_of_name=[]
    for category in categories:
        spending=sum([entry['amount'] for entry in category.ledger if entry['amount']<=0 ])
        category_vise_spending_dictionary[category.name]=((spending/total_spending)*100)//10*10
        len_of_name.append(len(category.name))

    #print(category_vise_spending_dictionary)

    for y_label in range(100,-1,-10):
        output+=f"\n{' '*(3-len(str(y_label)))}{y_label}|"
        for category in category_vise_spending_dictionary:
            if y_label<=category_vise_spending_dictionary[category]:
                output+=" o "
            else:
                output+="   "
    output+='\n    -'
    output+="---"*len(categories)
    output+="\n    "
    for index in range(max(len_of_name)):
        for category in category_vise_spending_dictionary:
            try:
                output+=f" {category[index]} "
            except:
                output+="   "
            
            
        output+="\n    "
        
    return output

chart=create_spend_chart([clothing,food,shit])
#for line in chart.split("\n"):
#    print(len(line))
print(chart)

Your browser information:

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

Challenge Information:

Build a Budget App - Build a Budget App

all lines should be long the same, maybe you have a spacing difference?

in the browser there is a more detailed output

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

the first is yours, the second is the expected, you have spacing issues

also the last line should be the one with the last letter of the longest word, there should not be an empty line

I do not know why that might be happening. I changed the line to

for category in category_vise_spending_dictionary:

            if y_label<=category_vise_spending_dictionary[category]:

                if categories[-1].name!=category:

                    output+=" o "

                else:

                    output+=" o"

            else:

                output+="   "

Can you also tell me how can I find the spacing in the same format as you showed? It seems really useful. Sorry to bug and thank you again

it appears in the browser console when you run the tests

also you can use repr when you print to see the string on one line similar to that

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

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

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