Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

I can’t seem to figure out the issue with my create spend chart function. The calculations are accurate, and as far as I can see, the spacing is accurate as well. I’m also having trouble understanding the output in the developer tools window. Please Help

Your code so far

class Category:
    def __init__(self,category):
        self.category = category
        self.ledger = []
        self.balance = 0
    def __str__(self):
        title = self.category.center(30,'*') + '\n'
        body = [f"{item['description'][:23]:23}{item['amount']:>7.2f}" for item in self.ledger]
        output = title + '\n'.join(body) + '\n' +'Total: ' + str(self.balance)
        return output.strip()
    def check_funds(self,amount):
        if self.balance >= amount:
            return True
        return False

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

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

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

    def get_balance(self):
        return self.balance
        


def create_spend_chart(categories):
    total = -sum([sum(expense['amount'] for expense in category.ledger if expense['amount'] < 0) for category in categories])
    percentages = [
        int(-sum(expense['amount'] for expense in category.ledger if expense['amount'] < 0) / total * 100)
    
        for category in categories
    ]
    

    category_names = [category.category for category in categories]
    

    lines = [
        f'{index:>3}| ' + '  '.join(['o' if percentage >= index else ' ' for percentage in percentages]) for index in range(100, -10, -10)]
    title = 'Percentage spent by category'
    body = ''
    for line in lines:
        body += line
        body += '\n'

    dashes = '    ' + '-' * (len(category_names) * 3 + 1)
    maximum = len(max(category_names, key=len))
    labels = ''
    for i in range(maximum):
        row = '     '
        for name in category_names:
            if len(name) > i:
                row += name[i] + '  '
            else:
                row += '   '
        labels += row + '\n'
    
    output = f'{title}\n{body}{dashes}\n{labels}'
    print(output)
    return output
food = Category('Food')
food.deposit(900, 'deposit')
food.withdraw(45.67, 'milk, cereal, eggs, bacon, bread')

entertainment = Category('Entertainment')
entertainment.deposit(500, 'deposit')
entertainment.withdraw(150, 'movies')

business = Category('Business')
business.deposit(1000, 'deposit')
business.withdraw(300, 'supplies')

random = Category('Random')
random.deposit(3000,'deposit')
random.withdraw(500,'withdraw')

create_spend_chart([food, business,entertainment,random])

    


Your browser information:

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

Challenge Information:

Build a Budget App Project - Build a Budget App Project

Press F12 to open the console. You’ll see the difference between your output (first line) and the expected output (second line). It seems you have additional spaces at the end of several lines:
image

Thanks so much. This helped me figure it out

1 Like