Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

I need help with number 17 it says creat_spend_chart should print a different chart representation check all spacing is exact. I’ve tried fixing but nothing is happening. here’s my code.
class Category:
def init(self, name):
self.name = name
self.ledger =

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

def withdraw(self, amount, description=""):
    if self.check_funds(amount):

Your code so far

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

    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):
        total = sum(item['amount'] for item in self.ledger)
        return total

    def transfer(self, amount, category):
        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):
        return amount <= self.get_balance()

    def __str__(self):
        title = f"{self.name:*^30}"
        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 + "\n" + items + total


def create_spend_chart(categories):
    total_spent = 0
    category_spent = {}

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

    # Calculate percentages
    percentages = {name: int((spent / total_spent) * 100) // 10 * 10 for name, spent in category_spent.items()}

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

    chart += "    ----------\n"

    # Prepare category names
    max_length = max(len(name) for name in percentages)
    for i in range(max_length):
        chart += "     "
        for name in percentages:
            chart += name[i] + "  " if i < len(name) else "   "
        chart += "\n"

    return chart.strip()

# Example usage
food = Category('Food')
food.deposit(1000, 'initial 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)
print(clothing)

# Create spend chart example
print(create_spend_chart([food, clothing]))

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) 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

have you tried checking the browser console like it say? You are almost there

The browser console says the same thing:

create_spend_chart

should print a different chart representation. Check that all spacing is exact.

It also shows an assertion error that shows the difference

AssertionError: 'Perc[362 chars]           m  \n           e  \n           n  \n           t'
             != 'Perc[362 chars]           m  \n           e  \n           n  \n           t  '

the first line is your output, the second is the expected. There is some difference at the end

there is also the diff, it shows the difference in a different way

  Percentage spent by category
  100|          
   90|          
   80|          
   70|    o     
   60|    o     
   50|    o     
   40|    o     
   30|    o     
   20|    o  o  
   10|    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  
             n  
-            t
+            t  
?             ++

a diff will have your line with a - in front, the expected line with a +, and a line that starts with ? showing the difference

Press F12 and check the devtools console in the browser

1 Like

I still don’t understand what’s wrong

Can you locate and share the error that you see there?

please replace
return chart.strip()
to
return chart.strip(“\n”)

you are missing two spaces on the last line

// running tests

  1. Your code raised an error before any tests could run. Please fix it and try again.

where would I put those spaces?

Create spend chart example

print(create_spend_chart([food, clothing]))

In order to troubleshoot this problem: first you need to press F12 and look in the browser console for this error

Once you can locate that we can help you interpret this error.

This is what is referred to here: