Build a Budget App - Build a Budget App

Tell us what’s happening:

Failing final two tests despite spacing being tested correctly.

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*-1, 'description': description
            })
            return True
        else:
            return False
    
    def get_balance(self):
        balance = 0.0
        for transaction in self.ledger:
            balance += float(transaction["amount"])
        return balance

    
    def transfer(self, amount, other):
        if self.check_funds(amount):
            self.withdraw(amount, f"Transfer to {other.name}")
            other.deposit(amount, f"Transfer from {self.name}")
            return True
        else:
            return False
    
    def check_funds(self, amount):
        balance = self.get_balance()
        if (float(balance) - float(amount)) < 0:
            return False
        else:
            return True

    def __str__(self):
        line_length = 30 - len(self.name)
        star = "*" * (line_length//2)

        first_line=f"{star}{self.name}{star}\n"
        lines = ''
        for transaction in self.ledger:
            lines += f"{transaction['description'][:23]:<23}{transaction['amount']:>7.2f}\n"
        lines = lines.strip()
        balance = self.get_balance()
        total = f"\nTotal: {balance}"
        return(first_line + lines + total)


def create_spend_chart(categories):
    title = "Percentage spent by category\n"
    percentages = []
    total_spent = 0
    spent = []
    for category in categories:
        category_spent = 0
        for transaction in category.ledger:
            if transaction["amount"] < 0:
                total_spent -= transaction["amount"]
                category_spent -= transaction["amount"]
        #print(category_spent)
        spent.append((category_spent, category.name))

    for pair in spent:
        pct = pair[0] / total_spent
        #print(pct)
        rounded = (int(pct * 10) )* 10
        #print(rounded)
        percentages.append({
            'Percent': (rounded), 'Category': pair[1]
        })
    chart_lines = ''
    chart_lines += title
    for i in range(100, -1, -10):
        line = f"{i:>3}|"
        for perc in percentages:
            if i <= perc["Percent"]:
                line += " o "
            else:
                line += "   "
        line += " \n"
        chart_lines += line
    chart_lines += "    " + "-" * (len(categories) * 3 + 1) + "\n"
    
    max_length = 0
    for category in categories:
        if len(category.name) > max_length:
            max_length = len(category.name)
    for i in range(max_length):
        line = "     "
        for cat in categories:
            if i < len(cat.name):
                line += cat.name[i] + "  "
            else:
                line += "   "
        chart_lines += line.rstrip() + " \n"
        
        #chart_lines += line
    chart_lines = chart_lines.strip()

    return(chart_lines)


food = Category('Food')
food.deposit(900, 'deposit')
food.withdraw(45.67, 'milk, cereal, eggs, bacon, bread')
clothing = Category('Clothing')
food.transfer(50, clothing)
print(clothing.get_balance())
print(food.get_balance())
#print(food.ledger)
auto = Category('Auto')
auto.deposit(500, 'deposit')
auto.withdraw(200, 'crash')
house = Category('House')
house.deposit(100, 'deposit')
house.withdraw(50, 'home')
categories = [food,clothing, auto, house]
clothing.withdraw(22.15, "boots")


print(create_spend_chart(categories))

"""
food = Category('Food')
food.deposit(1000, 'deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
print(clothing.get_balance())
#print(food)
"""

Your browser information:

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

Challenge Information:

Build a Budget App - Build a Budget App

Did you check the more detailed errors in the browser console (f12) ?

from the browser console you can see userful error messages like

AssertionError: 'Perc[226 chars]F  E \n     u  o  n \n     s  o  t \n     i  d[134 chars]   t'
             != 'Perc[226 chars]F  E  \n     u  o  n  \n     s  o  t  \n     i[148 chars] t  '

the top is your code, the bottom is the expected output
it looks like there is a mismatch with the spaces