Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

I can’t figure out the spacing for the last test. I believe I have everything generating correctly, but I just can’t get my output to pass. It’s not pretty but it is working.

Your code so far

class Category:
    def __init__(self, name):
        self.name = name
        self.ledger = []
        self.balance = 0
    
    def __str__(self):
        category_str = ''
        
        #add the name to the string
        half_length = len(self.name) // 2
        number_of_stars = 15 - half_length
        for _ in range(number_of_stars):
            category_str += '*'
        category_str += self.name
        for _ in range(number_of_stars):
            category_str += '*'
        category_str += '\n'
        
        #add ledger items
        for entry in self.ledger:
            amount, description = entry.values()
            #pad the description
            if len(description) < 23:
                number_of_spaces = 23 - len(description)
                for _ in range(number_of_spaces):
                    description += ' '
            else:
                description = description[:23]
            
            #pad the amount
            amount_str = str(amount)
            if amount_str.find('.') == -1:
                amount_str += '.00'
            
            if len(amount_str) < 7:
                for _ in range(7 - len(amount_str)):
                    amount_str = ' ' + amount_str
            else:
                amount_str = amount_str[:7]
                
            category_str += description + amount_str + '\n'
            
        #get the total line
        category_str += f'Total: {self.balance}'
        
        return category_str
        
    def deposit(self, amount, description = ''):
        self.ledger.append({'amount': amount, 'description': description})
        self.balance += amount
        
    def withdraw(self, amount, description = ''):
        if self.check_funds(amount):
            self.ledger.append({'amount': amount * -1, 'description': description})
            self.balance -= amount
            return True
        return False
        
    def get_balance(self):
        return self.balance
        
    def transfer(self, amount, other_category):
        if self.check_funds(amount):
            self.withdraw(amount, f'Transfer to {other_category.name}')
            other_category.deposit(amount, f'Transfer from {self.name}')
            return True
        return False
        
    def check_funds(self, amount):
        if amount <= self.balance:
            return True
        return False


def create_spend_chart(categories):
    bar_chart = ''
    percentages = {}
    for category in categories:
        percentages[category.name] = round(sum([entry['amount'] for entry in category.ledger if entry['amount'] < 0]), 2) * -1

    sum_of_withdraws = sum(percentages.values())
    
    for category, value in percentages.items():
        percentages[category] = round((percentages[category] / sum_of_withdraws) * 100, 2)
    #print(percentages)

    #create strings for output
    header = 'Percentage spent by category\n'
    str_100 = '100|'
    str_90 = ' 90|'
    str_80 = ' 80|'
    str_70 = ' 70|'
    str_60 = ' 60|'
    str_50 = ' 50|'
    str_40 = ' 40|'
    str_30 = ' 30|'
    str_20 = ' 20|'
    str_10 = ' 10|'
    str_0 = '  0|'
    
    for value in percentages.values():
        if value > 100:
            str_100 += ' o '
        else:
            str_100 += '   '
        if value > 90:
            str_90 += ' o '
        else:
            str_90 += '   '
        if value > 80:
            str_80 += ' o '
        else:
            str_80 += '   '
        if value > 70:
            str_70 += ' o '
        else:
            str_70 += '   '
        if value > 60:
            str_60 += ' o '
        else:
            str_60 += '   '
        if value > 50:
            str_50 += ' o '
        else:
            str_50 += '   '
        if value > 40:
            str_40 += ' o '
        else:
            str_40 += '   '
        if value > 30:
            str_30 += ' o '
        else:
            str_30 += '   '
        if value > 20:
            str_20 += ' o '
        else:
            str_20 += '   '
        if value > 10:
            str_10 += ' o '
        else:
            str_10 += '   '
        if value >= 0:
            str_0 += ' o '
        else:
            str_0 += '   '
            
    str_100 += ' \n'
    str_90 += ' \n'
    str_80 += ' \n'
    str_70 += ' \n'
    str_60 += ' \n'
    str_50 += ' \n'
    str_40 += ' \n'
    str_30 += ' \n'
    str_20 += ' \n'
    str_10 += ' \n'
    str_0 += ' \n'

    bottom_bar = '    '
    for _ in range(len(str_0) - 5):
        bottom_bar += '-'
    bottom_bar += '\n'
    
    vertical_names_str = ''
    longest_name_length = max([len(category.name) for category in categories])
    #print(longest_name_length)
    
    for i in range(longest_name_length):
        vertical_names_str += '    '
        for category in categories:
            if i < len(category.name):
                vertical_names_str += ' ' + category.name[i] + ' '
            else:
                vertical_names_str += '   '
        vertical_names_str += ' \n'
    
    #combine everything, print it, return it
    bar_chart = header + str_100 + str_90 + str_80 + str_70 + str_60 + str_50 + str_40 + str_30 + str_20 + str_10 + str_0 + bottom_bar + vertical_names_str
    print(bar_chart)
    return bar_chart

Your browser information:

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

Challenge Information:

Build a Budget App Project - Build a Budget App Project

Open the browser console with F12 to see a more verbose output of the tests.

Yes. I am aware of that. The console output is not helpful. Does it mean I’m missing spaces or have to many? Maybe the \n?

Never mind I figured it out. Turns out I did have a new line after the last letter in the last entry that I didn’t need.

1 Like

Here’s a primer for the next time this comes up:

An assertion error or diff output 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

- 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!