Build a Budget App - 19, 20, 22 & 24

Tell us what’s happening:

I have 19, 20,22, and 24 failing.
I don’t understand where ive gone wrong with the data display.
Everything looks solid to me in the code, but its not displaying correctly.
I also have some bugs i pointed out in the comments.

Your code so far


Your browser information:

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

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

I’ve fixed the data display issue, it now displays correctly…but I accidentally deleted my code by not saving it before i pressed the ask for help button, and for some reason i cannot see my code in this question. It’s just a blank single line block. Hopefully you can see it displayed

Your code was too long to be automatically inserted by the help button.

Please update the message, or reply to this one, to include your code, formatted as follows:

There are two ways you can format your code to make it easier to read and test:

  1. After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)

  1. Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.

To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:

EDIT: This is my code with only 22 & 24 failing.

import math

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": -amount, "description": description})

        return True

    else:

        return False



def get_balance(self):

    balance = 0

    for transaction in self.ledger:

        balance += transaction\['amount'\] 

    return balance



def check_funds(self, amount):

    return amount <= self.get_balance()



def transfer(self, amount, destination_category):

    if self.check_funds(amount):

        self.withdraw(amount, f"Transfer to {destination_category.name}")

        destination_category.deposit(amount, f"Transfer from {self.name}")

        return True

    else:

        return False

def \__str_\_(self):

    method = '\\n'.join(f"{transaction\['description'\]:23.23}{transaction\['amount'\]:>7.2f}" for transaction in self.ledger )

    return f"{self.name.center(30,'\*')}\\n{method}\\nTotal: {self.get_balance()}"

def \__iter_\_(self):

    for item in self.ledger:

        return item

def create_spend_chart(categories):

###################################    

#create a sum of all the withdraws from every category 

total_withdraw = 0 

amounts = \[transaction\['amount'\] for category in categories for transaction in category.ledger\] 

for amount in amounts:  

    if amount < 0:

        total_withdraw += -amount

#print(total_withdraw)   #  658.0699999999999  #####theres a square grey box that appears in the comment behind this one. It appears on multiple comments, I commented the entire script to understand it better and it pops up throughout multiple parts. it doesn't effect anything it also disappears if you press the space button 2x like i did

#####################################

#create a dictionary that holds the sum of withdraws for each category

c_w = {}  

for category in categories:  

    withdrawal = 0 

    for transaction in category.ledger: 

        if transaction\['amount'\] < 0: 

            withdrawal += -transaction\['amount'\] 

    c_w\[category.name\] = withdrawal

#print(c_w)    #{'Food': 188.07, 'Clothing': 360, 'Auto': 110}

#######################################################################

#create a list of the category names  

drop_down = \[\]

#create a dictionary of the category names and the percentage of the expenses of each category to the total of all expenses 

percentages = {} 

for Name, Amount in c_w.items():

    percentage = math.floor((Amount/total_withdraw)\*100 / 10)\*10 #do the math do get the percentage. Divide by 10 to make it singe digits, so when you floor it it gets rid of the decimal and excess and you can multiply by ten to return it to the nearest 10 place BELOW it or DOWN specifically instead of the nearest one.  

    percentages\[Name\] =  percentage

    drop_down += \[Name\]

    #print(percentages) #('Food', 30)\\n('Clothing', 50)\\n('Auto', 20)

#print(drop_down) #\['Food', 'Clothing', 'Auto'\]

################################################### 

#create the inital body for the chart

chart = ''  

header = 'Percentage spent by category'

chart += header + '\\n' 

bottom_line = "    " + "-" \* (len(c_w) \* 3 + 1) + '\\n'

#creating the y-axis

for num in range(100, -1, -10):  

    label = f"{num:3}| " 

    for Name in percentages.items(): 

#adding the bar details        

        if Name\[1\] >= num: 

            label += "o  "  

        else: 

            label += "   " 

    chart += label + "\\n"  

chart += bottom_line

#print(chart) #comes back in the right shape, but with incorrect data

#create the vertical name display

#print(chart) #names are displayed correctly, but it still has the wrong data  

longest_name_length = max(len(name) for name in drop_down) 

for i in range(longest_name_length):  

    line = "     "  

    for name in drop_down:

        if i < len(name): 

            line += name\[i\] + "  "

        else:

            line += '   '  

            

    chart += line + '\\n' 

     

return chart

food = Category(‘Food’)

auto = Category(‘Auto’)

food.deposit(1000, ‘initial deposit’)

auto.deposit(1000, ‘initial deposit’)

food.withdraw(15.74, ‘groceries’)

food.withdraw(21.34, ‘restaurant and more food for dessert’)

clothing = Category(‘Clothing’)

food.transfer(50, clothing)

food.withdraw(100.99, ‘bada bing’)

clothing.withdraw(20, ‘ding dong’)

auto.withdraw(110)

clothing.deposit(1000, ‘shit’)

clothing.withdraw(340, ‘fuck’)

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

this isn’t the original code, but it is the updated one.

17-24 are failing. it shows everything right on the console though. all except for the ‘none’ at the bottom, i actually just noticed that, im not sure why it does that

before everything was clearing, but it didnt have the right data in it it would only show 'o’s for auto on only two of the bars, not 3

EDIT

i just got rid of the none, that was a silly mistake. i returned it and its now only 22 and 24.

EDIT

NVM I figured it out, it passed. It shouldn’t be at the END i didn’t think it would work that way.