Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

Hello, Could someone look into my code and give me a hint please, i got stuck here for a couple of days, but, i still can’t find the solution correctly for the create spend chart function.

Your code so far

class Category:
    def __init__(self, category):
        self.category = category
        self.ledger = []
        self.budget = 0

    #the deposit method
    def deposit(self, amount, description=''):
        if description is None:
            return description
        else:
            self.ledger.append({'amount': amount, 'description': description})
        self.budget += amount


    #the withdraw method
    def withdraw(self, amount, description=''):
        if not self.check_funds(amount):
            return False
        else:
            self.budget -= amount
            self.ledger.append({'amount': -abs(amount), 'description': description})
            return True
        

    #the get balance method
    def get_balance(self):
        return self.budget

    #the transfer method
    def transfer(self, amount, other_budget):
        if self.check_funds(amount):
            self.withdraw(amount, f'Transfer to {other_budget.category}')
            other_budget.deposit(amount, f"Transfer from {self.category}")
            return True
        return False
        
    #the check_funds methos
    def check_funds(self, amount):
        if amount > self.budget:
            return False
        return True
        
    def __str__(self):
        result = ''
        title = self.category.center(30, '*')
        result += f'{title} \n'
        for item in self.ledger:
            description = item["description"]
            amount = item["amount"]
            result += f"{description[:23]} {amount:7.2f}\n"
        result += f"Total: {self.budget: .2f}"
        return result

#create spend chart
def create_spend_chart(categories):
    amount_spent = []
    for cat in categories:
        spend = 0
        for item in cat.ledger:
            if item["amount"] < 0:
                spend += abs(item["amount"])
        amount_spent.append(round(spend, 2))
    
    #get the total amount spend
    total = round(sum(amount_spent, 2))
    #get the percentage for each category
    percentage_spent = list(map(lambda amount: int((amount / total) * 100), amount_spent))
    
    title = "Percentage spent by category \n"

    chart = ""   
    for i in range(100, -1, -10):
        chart += f"{str(i) + '|':>4}"
        for percent in percentage_spent:
            if percent >= i:
                chart += ' o '
            else:
                chart += "   "
        chart += "\n"

    
    footer = "    " + "-" * ((len(categories) * 3) + 1) + "\n"

    category_names = [category.category for category in categories]

    max_length = max(len(name) for name in category_names)
    all_names = [name.ljust(max_length) for name in category_names]

    for i in range(max_length):
        footer += "     "
        for name in all_names:
            footer += name[i] + "  "
        footer += "\n"
    
    return title + chart + footer.rstrip('\n')


food = Category('Food')
food.deposit(900, 'deposit')
food.withdraw(45.67, 'groceries')
food.withdraw(200, 'drink')

clothing = Category('Clothing')
clothing.deposit(1200, 'deposit')
clothing.transfer(200, food)
clothing.withdraw(300)

auto = Category('Auto')
auto.deposit(2000, 'deposit')
auto.transfer(500, clothing)
auto.withdraw(500, 'Sports')

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



Your browser information:

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

Challenge Information:

Build a Budget App Project - Build a Budget App Project

This is the message i got when trying to debug it using the console:

AssertionError: 'Perc[19 chars]egory \n100|         \n 90|         \n 80|    [345 chars] t  ' 
             != 'Perc[19 chars]egory\n100|          \n 90|          \n 80|   [355 chars] t  '`

I notice one has a space after y, before the first \n, the other doesn’t

What do you mean? I have been struggling with this for days

What do you mean by that?

in this assertion Error you posted. The first string, yours, has a space just after “egory”, the expected string doesn’t have that space. Differences like this are what failing your output

I have edited your post to format it, now it’s more visible

Thank you! Let me try to see what is happening!

When i remove the space the first 100| goes to the same line with the title.

you should remove only the space, not the newline character

I have done that, but does work:
This is my code now:
#create spend chart
def create_spend_chart(categories):
amount_spent =
for cat in categories:
spend = 0
for item in cat.ledger:
if item[“amount”] < 0:
spend += abs(item[“amount”])
amount_spent.append(round(spend, 2))

#get the total amount spend
total = round(sum(amount_spent, 2))
#get the percentage for each category
percentage_spent = [int((amount / total)*100) for amount in amount_spent]

title = "Percentage spent by category\n"

chart = ""   
for i in range(100, -1, -10):
    chart += f"{str(i) + '|':>4}"
    for percent in percentage_spent:
        if percent >= i:
            chart += ' o '
        else:
            chart += "   "
    chart += "\n"


footer = "    " + "-" * ((len(categories) * 3) + 1) + "\n"

category_names = [category.category for category in categories]

max_length = max(len(name) for name in category_names)
all_names = [name.ljust(max_length) for name in category_names]

for i in range(max_length):
    footer += "     "
    for name in all_names:
        footer += name[i] + "  "
    footer += " \n"

return title + chart + footer.rstrip(' \n')

This is the new error message i got:

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

After a couple of minutes working on it: This is my new error, it seems like thing is about to work but i don’t know what to do now for it to work:

AssertionError: 'Perc[77 chars]|          \n 60|    o     \n 50|    o     \n [297 chars] t  ' 
             != 'Perc[77 chars]|    o     \n 60|    o     \n 50|    o     \n [297 chars] t  '

you need to format your code to make it show the spaces, otherwise spaces collapse

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

now it seems that your columns have the wrong number of circles. Double check how you are calculating the heights of the columns

Thank you, i’m working on that!

I don’t know what is happening but my code is still not working
Even when i tried a lot of thing, it still gives me same error.

This is my code the result i got :
100|          
 90|          
 80|          
 70|          
 60| o        
 50| o        
 40| o        
 30| o  o     
 20| o  o     
 10| o  o     
  0| o  o  o  
    ----------
     F  c  A  
     o  l  u  
     o  o  t  
     d  t  o  
        h     
        i     
        n     
        g 

batched @ pyodide.asm.js:9

This is the error:
AssertionError: 'Perc[77 chars]|          \n 60|    o     \n 50|    o     \n [297 chars] t  ' != 'Perc[77 chars]|    o     \n 60|    o     \n 50|    o     \n [297 chars] t  '
batched @ pyodide.asm.js:9

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!

Format your error like this so it’s easier to see the difference in the two lines

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