Build a Budget App - Build a Budget App

Tell us what’s happening:

I can’t pass the last test. I tried with several options, and even replaced all the white space with tildes to make sure my spacing was correct. I’ve removed the final white line, so that isn’t it. I don’t understand the debugger, I don’t see how I can replicate the mistake it’s apparently making. Please help!

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': -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 transfer(self,amount,destination):
        if not self.check_funds(amount):
            return False
        else:
            self.withdraw(amount, f"Transfer to {destination.name}")
            destination.deposit(amount, f"Transfer from {self.name}")
            return True

    def check_funds(self,amount):
        if self.get_balance() >= amount:
            return True
        else:
            return False
    
    def truncate(self, string, length):
        return str(string)[:length]

    def __str__(self):
        result = ""
        total = self.get_balance()
        title = self.name.center(30, "*")
        result += title + '\n'
        for entry in self.ledger:
            right = f"{entry['amount']:.2f}"
            left = self.truncate(entry['description'],23)
            right = self.truncate(right,7)
            result += left+" "*(30-len(left)-len(right))+ right + "\n"
        result += 'Total: '+ f'{total:.2f}'
        
        return result
    

        
#This is where the fun begins...    
def create_spend_chart(categories):
    cat_withdrawal = {}
    perc_withdrawal = {}
    chartstr = ""

    total = 0
    for i in categories:
        withdrawal = 0
        for x in i.ledger:
            if x['amount'] < 0:
                withdrawal += abs(x['amount'])
        total += withdrawal
        cat_withdrawal[i.name] = withdrawal

    for index in cat_withdrawal:
        perc_withdrawal[index] = int(f"{cat_withdrawal[index]/total*10:.0f}")*10

    
    chartstr += "Percentage spent by category\n"
    for p in range(100,-10,-10):
        if p == 100:
            chartstr += str(p) + "| "
        elif p == 0:
            chartstr += "  " + str(p) + "| "
        else:
            chartstr += " " + str(p) + "| "
        for index in cat_withdrawal: 
            if p <= cat_withdrawal[index]:
                chartstr+= "o  "
            else:
                chartstr += "   "
        chartstr += "\n" 
    
    count = 0
    for i in categories:
            count += 1
    chartstr += "    "+"-"*(count*3+1)+ "\n"

    list = [i.name for i in categories]
    catlength = 0
    for i in list:
        if len(i) > catlength:
            catlength = len(i)

    
    x=0
    for i in range(catlength):
        chartstr += "     "
        for item in list:
            if x < len(item):
                chartstr += item[x]+'  '
                
            else:
                chartstr +='   '
        x += 1
        chartstr += '\n'
    chartstr = chartstr[0:-1]
        
    return chartstr

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:150.0) Gecko/20100101 Firefox/150.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

Welcome to the forum @LiLuStitch!

I tested using these transactions and function call:

    categories = []
    food = Category('Food')
    categories.append(food)
    food.deposit(1000,'deposit')
    food.withdraw(10.15,'groceries')
    food.withdraw(15.89,'restaurant and more food for dessert')
    clothing = Category('Clothing')
    categories.append(clothing)
    food.transfer(50, clothing)
    clothing.deposit(800,'deposit')
    clothing.withdraw(55.20,'jacket')
    auto = Category('Auto')
    categories.append(auto)
    auto.deposit(500,'deposit')
    auto.withdraw(42.39,'fuel')
    auto.withdraw(78.62,'oil change')
    for cat in categories:
        print(cat)
    print(create_spend_chart(categories))

Your code print this chart:

Percentage spent by category
100|       o  
 90|       o  
 80|       o  
 70| o     o  
 60| o     o  
 50| o  o  o  
 40| o  o  o  
 30| o  o  o  
 20| o  o  o  
 10| o  o  o  
  0| o  o  o  
    ----------
     F  C  A  
     o  l  u  
     o  o  t  
     d  t  o  
        h     
        i     
        n     
        g

But it should print this chart instead:

Percentage spent by category
100|          
 90|          
 80|          
 70|          
 60|          
 50|       o  
 40|       o  
 30|       o  
 20| o  o  o  
 10| o  o  o  
  0| o  o  o  
    ----------
     F  C  A  
     o  l  u  
     o  o  t  
     d  t  o  
        h     
        i     
        n     
        g

So, it looks like there is something wrong with the way you are calculating withdrawal percentages.

If you open your browser’s console, you can see more information about the error. It’s a bit cryptic, but basically the - indicates your code’s output and the + indicates what the test expects.

Happy coding!

Thank you so much, I figured it out! I made a typo in my percentage calculations and I rounded regularly instead of down to the nearest 10. That did it :smile: