Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

My code works, but i still keep on getting error.
It says to check if all spacing is exact and to open browser console for more details.

Your code so far

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

    def deposit(self, amount, description=""):
        """Adds a deposit to the ledger."""
        self.ledger.append({'amount': amount, 'description': description})

    def withdraw(self, amount, description=""):
        """Withdraws an amount from the ledger if sufficient funds exist."""
        if self.check_funds(amount):
            self.ledger.append({'amount': -amount, 'description': description})
            return True
        return False

    def get_balance(self):
        """Returns the current balance of the category."""
        balance = sum(item['amount'] for item in self.ledger)
        return balance

    def transfer(self, amount, category):
        """Transfers an amount to another category if sufficient funds exist."""
        if self.check_funds(amount):
            self.withdraw(amount, f'Transfer to {category.name}')
            category.deposit(amount, f'Transfer from {self.name}')
            return True
        return False

    def check_funds(self, amount):
        """Checks if sufficient funds exist for a withdrawal or transfer."""
        return amount <= self.get_balance()

    def __str__(self):
        """Returns a string representation of the category's ledger."""
        title = f"{self.name:*^30}\n"
        items = ""
        for item in self.ledger:
            description = item['description'][:23]
            amount = f"{item['amount']:.2f}"
            items += f"{description:<23}{amount:>7}\n"
        total = f"Total: {self.get_balance():.2f}"
        return title + items + total


def create_spend_chart(categories):
    """Creates a spend chart from a list of categories."""
    total_spent = 0
    category_spent = {}

  
    for category in categories:
        spent = sum(-item['amount'] for item in category.ledger if item['amount'] < 0)
        total_spent += spent
        category_spent[category.name] = spent

    
    percentage_spent = {name: (spent / total_spent) * 100 for name, spent in category_spent.items()}

  
    chart = "Percentage spent by category\n"
    for i in range(100, -1, -10):
        chart += f"{i:>3}| "  
        for name in percentage_spent:
            if percentage_spent[name] >= i:
                chart += "o  "  
            else:
                chart += "   " 
        chart += "\n"  

  
    chart += "    " + "-" + "---" * len(percentage_spent) + "\n"

    max_length = max(len(name) for name in percentage_spent)
    for i in range(max_length):
        chart += "     "  
        for name in percentage_spent:
            if i < len(name):
                chart += name[i] + " "  
            else:
                chart += "   "  
        chart += "\n"  

    return chart.rstrip()  

    


if __name__ == "__main__":
    food = Category("Food")
    food.deposit(1000, "Initial deposit")
    food.withdraw(200, "Groceries")
    food.withdraw(150, "Dining out")

    clothing = Category("Clothing")
    clothing.deposit(500, "Initial deposit")
    clothing.withdraw(50, "T-shirt")

    entertainment = Category("Entertainment")
    entertainment.deposit(300, "Initial deposit")
    entertainment.withdraw(90, "Movies")

    print(food)
    print(clothing)
    print(entertainment)
    
   

    
  

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

Press F12 and check the browser console for a more verbose error

Mod Edit: SOLUTION REMOVED

  • Fixed the percentage line spacing:
  • Numbers are right-aligned with 3 spaces
  • Followed by "| "
  • Each “o” or space is followed by two spaces
  • Fixed the horizontal line:
  • Starts with 4 spaces
  • Followed by one “-”
  • Then “—” for each category
  • Fixed vertical category names:
  • Each line starts with 5 spaces
  • Each letter is followed by two spaces
  • No newline after the last line
  • Removed .rstrip() as it was removing needed whitespace

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

try your suggestion but won’t still work

Please post your updated code

I have done any changes to the code, cause after i modified it with the previous instructions it still didn’t work so i reverted the code back.

Press F12 and check the browser console for an error with more information

i cant seem to understand the error information that’s showing in the panel

The original code is wrong, so I would try to follow the suggestions above and show us what you try.

made the twicks and this is what am left with

def create_spend_chart(categories):
    """Creates a spend chart from a list of categories."""
    total_spent = 0
    category_spent = {}

    for category in categories:
        spent = sum(-item['amount'] for item in category.ledger if item['amount'] < 0)
        total_spent += spent
        category_spent[category.name] = spent

  
    percentage_spent = {name: (spent / total_spent) * 100 for name, spent in category_spent.items()}

    
    chart = "Percentage spent by category \n"
    for i in range(100, -1, -10):
        chart += f"{i:>3}| "  
        for name in percentage_spent:
            if percentage_spent[name] >= i:
                chart += "o" + "  "
            else:
                chart += "  " 
        chart += "\n" 

    
    chart += "   " + "-" + "---" * len(percentage_spent) + "\n"

    
    max_length = max(len(name) for name in percentage_spent)
    for i in range(max_length):
        chart += "     " 
        for name in percentage_spent:
            if i < len(name):
                chart += name[i] + "  "
            else:
                chart += "   " 
    return chart.strip()

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!