Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

cannot solve the code its gett

class Category():

    def __init__(self, name):
        self.name = name
        self.ledger = []
        self.total = 0.0
    
    def deposit(self, amount, description = ""):
        self.ledger.append({'amount': amount, 'description': description})
        self.total += amount

    def withdraw(self, amount, description = ""):
        if not self.check_funds(amount):
            return False
        else:    
            self.total += (amount*-1)
            self.ledger.append({'amount': round(amount*-1,2), 'description': description})
        return True


    def get_balance(self):
        return self.total

    def transfer(self, amount, budget):
        if self.withdraw(amount,f'Transfer to {budget.name}'):
            budget.deposit(amount,f'Transfer from {self.name}')
            return True
        else:
            return False

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

    def __repr__(self):
        outputstring = ''
        outputstring += f"{self.name:*^30}\n"
        end = 23
        final = 0.0

        for t in self.ledger:
            if len(t['description']) > 23:
                if type(t['amount']) == type(int()):
                    a = f"{t['amount']:.2f}"
                    outputstring += f"{t['description'][0:end]}{a:>{30 - len(t['description'][0:end])}}\n"
                else:
                    outputstring += f"{t['description'][0:end]}{t['amount']:>{30 - len(t['description'][0:end])}}\n"  

            elif type(t['amount']) == type(int()):
                a = f"{t['amount']:.2f}"
                outputstring += f"{t['description'][0:end]}{a:>{30 - len(t['description'][0:end])}}\n"

            else:    
                outputstring += f"{t['description']}{t['amount']:>{30 - len(t['description'])}}\n"
            final += t['amount']
        outputstring += f"Total: {final:.2f}"
        
        
        return outputstring
        
                
                




        


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, 'for food')
business.withdraw(33.40, 'for business')
entertainment.withdraw(10.99, 'for entertainment')


#print(food.get_balance())
#print(str(food))
#print(food.ledger)
#print(business.ledger)




def create_spend_chart(categories = []):

        
    total_withdraw = 0

    catl = {}

    chartstr = f"Percentage spent by category\n"
    

    for c in categories:
        ctotal = 0
        for i in c.ledger:
            if i['amount'] < 0:
                total_withdraw += i['amount']
                ctotal += abs(i['amount'])

        catl[c.name] = ctotal
    total_withdraw = abs(total_withdraw)

    
    for key in catl:
        percentage = (catl[key]/total_withdraw)*100
        catl[key] = percentage
        
    for p in range(100,-10,-10):    
        if p == 100:
            chartstr += f"{str(p) + '|'}"
        elif p == 0:
            chartstr += f"  {str(p) + '|'}"
        else:
            chartstr += f" {str(p) + '|'}"
        for value in catl.values():           
            if p <= value:
                chartstr += f' o ' 
            else:
                chartstr += f'   ' 
        chartstr += '\n'

    L = len(catl.items())
    chartstr += "    "f"{'-'*(L*3 + 1)}"
    chartstr += "\n"
     
        
    categorylength = 0
    for i in categories:
        if len(i.name) > categorylength:
            categorylength = len(i.name)

    for i in range(categorylength):
        chartstr += "    "
        for n in categories:
            if i < len(n.name):
                chartstr += ' ' + n.name[i] + ' '
            else:
                chartstr += "   "
        chartstr += " \n"

    

    print(catl)
    print(chartstr)
    return chartstr

    
    
   
create_spend_chart([business, food, entertainment])

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/131.0.0.0 Safari/537.36

Challenge Information:

Build a Budget App Project - Build a Budget App Project

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

Welcome to the forum @rebbacharlesphinehas

The assertion error and diff gives you a lot of information to track down a problem. For example:

AssertionError: 'Year' != 'Years'
- Year
+ Years
?     +

Your output comes first, and the output that the test expects is second.

AssertionError: ‘Year’ != ‘Years’

Your output: Year does not equal what’s expected: Years

This is called a diff, and it shows you the differences between two files or blocks of code:

- Year
+ Years
?     +

- Dash indicates the incorrect output
+ Plus shows what it should be
? The Question mark line indicates the place of the character that’s different between the two lines. Here a + is placed under the missing s .

Here’s another example:

E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E       assert '  3801      123    \n   - 2     + 49    \n------    -----    \n' == '  3801      123\n-    2    +  49\n------    -----'
E         -   3801      123
E         +   3801      123    
E         ?                ++++
E         - -    2    +  49
E         +    - 2     + 49    
E         - ------    -----
E         + ------    -----    
E         ?                +++++

The first line is long, and it helps to view it as 2 lines in fixed width characters, so you can compare it character by character:

'  3801      123    \n   - 2     + 49    \n------    -----    \n'
'  3801      123\n-    2    +  49\n------    -----'

Again, your output is first and the expected output is second. Here it’s easy to see extra spaces or \n characters.

E         -   3801      123
E         +   3801      123    
E         ?                ++++

Here the ? line indicates 4 extra spaces at the end of a line using four + symbols. Spaces are a little difficult to see this way, so it’s useful to use both formats together.

I hope this helps interpret your error!

Happy coding

1 Like