Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

I passed all checks, but stuck at the final function that displays the barchart (the final check). My chart seems to have met all the requirements, and I can’t figure out why it still didn’t pass. Maybe it’s the calculation of withdrawal percentages that I misunderstood?

Thank you =3

Your code so far

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

    def __str__(self):
        ledger_str = self.category.center(30, '*') + '\n'
        max_length = len(ledger_str)
        for item in self.ledger:
            description_text = item['description'].ljust(23, ' ') if len(item['description']) <= 23 else item['description'][:23]
            amount_text = f"{item['amount']:.2f}".rjust(7,' ')
            if len(amount_text) > 7:
                amount_text = amount_text[0:7]
            ledger_str += f"{description_text}{amount_text}\n"
        ledger_str += f"Total: {sum(item['amount'] for item in self.ledger)}"
        return ledger_str

    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
        return False
    
    def get_balance(self):
        # Calculate balance by summing all amounts in the ledger
        return sum(item['amount'] for item in self.ledger)

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

    def check_funds(self, amount):
        if sum(item['amount'] for item in self.ledger) >= amount:
            return True
        return False



food = Category("Food")
food.deposit(1000)
food.withdraw(100)

clothing = Category("Clothing")
clothing.deposit(1000)
clothing.withdraw(170)

college = Category("College Fund")
college.deposit(500)
college.withdraw(250)

drink = Category('Drink')
drink.deposit(1000)
drink.withdraw(500)

def create_spend_chart(categories):
    # Calculate total withdrawals and spending percentages
    withdrawn_list =[]
    total_spent = 0
    for cat in categories:
        category_withdrawn = sum(item['amount'] for item in cat.ledger if item['amount'] < 0)
        withdrawn_list.append(category_withdrawn)
        total_spent += category_withdrawn
    percentages = [(x/total_spent)*1000//10 for x in withdrawn_list] #withdraw% = cat.withdraw / total_withdraw?
    
    # Build the chart
    chart = 'Percentage spent by category\n'
    
    for i in range(100, -1, -10):
        chart += f"{i}| ".rjust(5, " ")  
        for percent in percentages:
            chart += 'o' if percent >= i else ' ' # Print 'o' if reach percentages, or ' ' if not
            chart += '  ' 
        chart += '\n'
    
    # Dash line
    chart += ' '*4 + '-' * (len(categories)*3 +1)+'\n'
    
    # Print category names
    max_name_length = max(len(cat.category) for cat in categories)
    for i in range(max_name_length):
        chart += '     '  # Padding before the category names
        for cat in categories:
            # Print the i-th character of each category's name, or a space if shorter
            chart += cat.category[i] if i < len(cat.category) else ' '
            chart += '  '
        chart += '\n'
    
    return chart

categories = [food, clothing, college, drink]
print(create_spend_chart(categories))


Your browser information:

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

Challenge Information:

Build a Budget App Project - Build a Budget App Project

If you open the browser console you will see more infos on why the test is failing.

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!

1 Like