Budget App Project -create_spend_chart method representation

Tell us what’s happening:

My only problem lies in the create_spend_chard method or rather it representation. The test states that my output and the correct output are not the same. However, diff viewers does not see a difference:
What to do?

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

Challenge Information:

Build a Budget App Project - Build a Budget App Project

Can you share your code or output or the devtools console error?

Nothing to go on here

Yeah sorry I failed to do so at the beginning and then my post was muted for some time because i wanted to post a link

Here’s the error message:

Ran 1 test in 0.000s


======================================================================
2
FAIL: test_create_spend_chart (test_module.UnitTests.test_create_spend_chart)
2
Traceback (most recent call last):
2
File "/home/pyodide/test_module.py", line 23, in test_create_spend_chart
2
self.assertEqual(actual, expected, 'Expected different chart representation. Check that all spacing is exact.')
2
AssertionError: 'Perc[34 chars] \n 90| \n 80| \n 70| o[329 chars] t ' != 'Perc[34 chars] \n 90| \n 80| \n 70| [340 chars] t '
2
Percentage spent by category

- 100|
+ 100|
? +
- 90|
+ 90|
- 80|
+ 80|
- 70| o
+ 70| o
- 60| o
+ 60| o
- 50| o
+ 50| o
- 40| o
+ 40| o
- 30| o
+ 30| o
- 20| o o
+ 20| o o
- 10| o o
+ 10| o o
- 0| o o o
+ 0| o o o
----------
B F E
u o n
s o t
i d e
n r
e t
s a
s i
n
m
e
t : Expected different chart representation. Check that all spacing is exact.
Ran 1 test in 0.005s
FAILED (failures=1)
Ran 1 test in 0.001s

AssertionError: 'Perc[34 chars] \n 90| \n 80| \n 70| o[327 chars] t' != 'Perc[34 chars] \n 90| \n 80| \n 70| [340 chars] t '
that’s the error message

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

1 Like

Can you share your full code please?

class Category:

    
    
    def __init__(self,category):
        
        self.category = category
        self.ledger = []
        
    
        
    def __str__(self):
        topic_line = self.category.center(30,'*')
        ledger_lines = ''
        for line in self.ledger:
            description_string = ''
            if len(line['description']) > 23:
                description_string = line['description'][:23]
            else:
                description_string = line['description']
            
            amount_string = str((line['amount']))

            if '.' not in amount_string:
                amount_string += '.00'
            if len(amount_string) > 7:
                amount_string = amount_string[:7]
            amount_string = amount_string.rjust(30-len(description_string))

            ledger_str = description_string + amount_string
            ledger_lines += ledger_str + '\n'
        
        return f'{topic_line}\n{ledger_lines}Total: {self.get_balance()}'
       
        
    
    def deposit(self, amount, description = ''):    
        deposit = {}
        deposit['amount'] = amount
        deposit['description'] = description
        self.ledger.append(deposit)


    def withdraw(self, amount,description =''):
        withdrawal = {}
        withdrawal['amount'] = -amount
        withdrawal['description'] = description
        if self.check_funds(amount) is False:
            return False
        else:
            self.ledger.append(withdrawal)
            return True
    
    def get_balance(self):
        balance = 0
        for changes in self.ledger:
            balance += changes['amount']
        return balance

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

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

    

def create_spend_chart(categories):
    category_withdrawals = {}
    category_percentages = {}
    category_name_length  = 0        
    category_names = []
    categories_amount = 0
    for category in categories:
        category_names.append(category.category)
        categories_amount += 1
        if len(category.category) > category_name_length:
            category_name_length = len(category.category)
    
    categories_total_withdrawals = 0
    for category in categories:
        
        category_withdrawals[category.category] = 0
        for transaction in category.ledger:
            if transaction['amount'] < 0:
                category_withdrawals[category.category] += transaction['amount']
                categories_total_withdrawals += transaction['amount']

    for category in categories:
        category_percentage = 100*(category_withdrawals[category.category]/categories_total_withdrawals)// 10
        category_percentages[category.category] = category_percentage*10
    
    percentage_display = 'Percentage spent by category\n'
    for percentage in range(100, 0-1, -10):
        percentage_display += str(percentage).rjust(3)+'|'
        for category in categories:
            if category_percentages[category.category]>= percentage:
                percentage_display += ' o '
            else:
                percentage_display += '   '
        percentage_display += '\n'
    percentage_display += '    -'

    for category in categories:
        percentage_display += '---'
   
    
    for i in range(0, category_name_length):
        percentage_display += '\n     '
                
        for name in category_names:
            
            if i < len(name):
                percentage_display += name[i] +'  '
            else:
                percentage_display += '   '
     
    return f'{percentage_display.rstrip()}'  
    
    
    
     
    


food = Category('Food')
clothing = Category('Clothing')
auto = Category('Auto')
food.deposit(1000, 'deposit')
clothing.deposit(1000)
auto.deposit(1000)
food.withdraw(601)
clothing.withdraw(250)
auto.withdraw(150)







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



Is that right? Because when I paste this code in I get this output:

<built-in method rstrip of str object at 0x10572b0>

and this console error:

AssertionError: '<built-in method rstrip of str object at 0x10b9db8>' != 'Percentage spent by category\n100|       [383 chars] t  '

I corrected it, sth went wrong when copy pasting, I’m sorry

When I use the example input:

food = Category('Food')
food.deposit(1000, 'deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
print(food)

I don’t get the full chart as in the example, I only see this:

Transfer to Clothing    -50.00
Total: 923.96

but the example says it should look like this:

*************Food*************
initial deposit        1000.00
groceries               -10.15
restaurant and more foo -15.89
Transfer to Clothing    -50.00
Total: 923.96

I checked with the code I posted and for me the category class works without problems. The only thing I’m wondering about is why it doesn’t accept the create_spend_chart method.

you have an issue with spaces, add this and see, the spaces in the output are replaced with dots. There is both your code and the expected one being printed.

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

actual = create_spend_chart([business, food, entertainment])
expected = "Percentage spent by category\n100|          \n 90|          \n 80|          \n 70|    o     \n 60|    o     \n 50|    o     \n 40|    o     \n 30|    o     \n 20|    o  o  \n 10|    o  o  \n  0| o  o  o  \n    ----------\n     B  F  E  \n     u  o  n  \n     s  o  t  \n     i  d  e  \n     n     r  \n     e     t  \n     s     a  \n     s     i  \n           n  \n           m  \n           e  \n           n  \n           t  "
print("actual" + "\n\n" + actual.replace(' ', '.') + '\n\n')
print("expected" + "\n\n" + expected.replace(' ', '.'))
2 Likes
- 100|         
+ 100|          
?              + 
-  90|         
+  90|          
?              + 
 

Right, can see this in the diff now as well, I didn’t see it before for some reason

1 Like

Thanks a lot! It worked

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.