Build a Budget App - Step 19

I can’t seem to get Step 19 to pass. The output looks good, the percentages are rounded down to the nearest 10. Can anyone help out? Thanks!


total_spent = 0 ; 



class Category:

    def \__init_\_(self, name):

        self.name = name

        self.ledger = \[\]

        self.spent = 0

    

    def deposit(self, amount, description=''):

        self.ledger.append({'amount': amount, 'description': description})



    def withdraw(self, amount, description=''):

        global total_spent

        if self.check_funds(amount):

            self.ledger.append({'amount': -amount, 'description': description})

            self.spent += amount

            total_spent += amount

            return(True)              

        else: 

            return(False)



    def get_balance(self):

        balance = 0

        for transaction in self.ledger:

            balance += transaction\['amount'\]

        return(balance)



    def transfer(self, amount, other_cat):      

        if self.check_funds(amount) == True:

            self.ledger.append({'amount': -amount, 'description': f"Transfer to {other_cat.name}"})

            other_cat.ledger.append({'amount': amount, 'description': f"Transfer from {self.name}"})            

            return(True)                    

        else: 

            return(False)




    def check_funds(self, amount):       

        if amount <= self.get_balance():

            return(True)

        else:

            return(False)



    def \__str_\_(self):

        self.stars_left = round((30 - len(self.name)) / 2) ;

        self.stars_right = round((30 - len(self.name)) / 2) ;

        self.title_line = '\*' \* self.stars_left + self.name + '\*' \* self.stars_right + '\\n'

        ledger_contents = ''

        for transaction in self.ledger:

            if "." in str(transaction\['amount'\]):

                amount = str(transaction\['amount'\])

            else:

                amount = str(transaction\['amount'\]) + '.00'  

            if len(transaction\['description'\]) >= 23:

                description = transaction\['description'\]\[:23\]

                spaces = 7 - len(amount)

            else:

                description = transaction\['description'\]

                spaces = 30 - len(description) - len(amount)

            ledger_contents += description + " " \* spaces + amount + "\\n"

        return self.title_line + ledger_contents + "Total: " + str(self.get_balance())



def create_spend_chart(categories):

    lines = 'Percentage spent by category\\n'

    nb_cat = len(categories)

    for i in range(100,-10,-10):

        current_line = str(i).rjust(3) + "|"

        for cat in categories:

            perc = 100 \* cat.spent / total_spent

            perc = int(perc/10) \* 10

            if i == 0:

                current_line += " o "

            elif (perc > 0 and perc % i == 0) or perc >=i:

                current_line += " o "

            else:

                current_line += "   "

        lines += current_line + '\\n'

    lines += "    " + "\_"\* nb_cat \* 3 + "\_\\n"

    max_cat_length = 0

    for cat in categories:

        if len(cat.name) > max_cat_length:

            max_cat_length = len(cat.name)



    for i in range(0, max_cat_length):

        current_line = '    '

        for cat in categories:

            if len(cat.name) >= i+1 :

                current_line += ' ' + cat.name\[i\] + ' '

            else:

                current_line += '   '

        lines += current_line + '\\n'

    return lines

    





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(300, 'pay day')

clothing.withdraw(158.39, "new outfit")

food.transfer(50, clothing)

print(food)

print('')

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

Sorry I’m not sure how to copy code into a forum post correctly, it definitely did not copy over well. How do I do that properly?

Welcome to the forum @aabdesse!

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 and test.

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 (').


In the future, if you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge.

The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

Happy coding!

Thanks for the help. There are however still random backslashes in the code

Yes. You can go ahead and use the “Help” button to create another topic where your code is formatted correctly and a link to the challenge is included.

you will need to post the correct code yourself, we are not able to guess what the original code was. The random backslashes appear when the code is not correctly formatted when teh post is first created

I’ve already created a new topic using the “Help” button