Build a Budget App - Build a Budget App

Tell us what’s happening:

whenever i run my code it shows “Title at the top of create_spend_chart chart should say Percentage spent by category” and I cant figure out why is this error occuring. Can someone please provide me a hint to fix this.

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 get_balance(self):
        total = 0
        for i in self.ledger:
            total += i['amount']
        return total

    def check_funds(self, amount):
        return amount <= self.get_balance()

    def withdraw(self, amount, description = ''):
        if self.check_funds(amount):
            self.ledger.append({'amount': -amount, 'description': description})
            return True
        return False

    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 __str__(self):
        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):
    title = "Percentage spent by category\n"
    spent = []
    for i in categories:
        total = 0 
        for item in i.ledger:
            if item["amount"] < 0:
                total += -item["amount"]
        spent.append(total)
    
    total_spent = sum(spent)
    percentages = []
    for s in spent:
        percentages.append((s / total_spent) * 100)
    percentages = [int((p // 10) * 10) for p in percentages]
    
    chart = title

    for i in range(100, -1, -10):
        line = f"{i:>3}  "
        for p in percentages:
            line += "o  " if p >= i else "   "
        chart += line + "\n"

    chart += "    " + "-" + (len(categories) * 3 + 1) + "\n"

    max_len = max(len(cat.name) for cat in categories)

    for i in range(max_len):
        line = "     "
        for cat in categories:
            if i < len(cat.name):
                line += cat.name[i] + "  "
            else:
                line += "   "
        chart += line
        if i < max_len - 1:
            chart += "\n"
    return chart

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36

Challenge Information:

Build a Budget App - Build a Budget App

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-budget-app/5e44413e903586ffb414c94e.md at main · freeCodeCamp/freeCodeCamp · GitHub

Welcome to the forum @anmolrustagi123,

How are you testing your code? I don’t see a function call.

I added this to the end of your code:

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(create_spend_chart([food,clothing]))

And see this in the console (note the error):

*************Food*************
initial deposit        1000.00
groceries               -10.15
restaurant and more foo -15.89
Transfer to Clothing    -50.00
Total: 923.96
Traceback (most recent call last):
  File "main.py", line 89, in <module>
  File "main.py", line 65, in create_spend_chart
TypeError: can only concatenate str (not "int") to str

Happy coding