Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

I am having trouble passing the last test and not sure of what is wrong with my output. It all seems in place and I’m also unsure of how to get the expected output so that I can compare them. Can I get some insight?

Your code so far

class Category:
    # Atributos de classe
    def __init__(self, name):
        self.name = name
        self.balance = 0
        self.ledger = []
        self.expenses = 0
        self.deposits = 0
    
    # Representação em string    
    def __str__(self):
        title = self.name.center(30, '*')
        registers = ''
        for register in self.ledger:
            description = register['description'][:23].ljust(23)
            amount = f'{register["amount"]:.2f}'.rjust(7)
            registers += f'{description}{amount}\n'
        total = f'Total: {self.balance}'
        return f'{title}\n{registers}{total}'
        
    def deposit(self, amount, description=''):
        self.balance += amount
        self.deposits += amount
        self.ledger.append({'amount': amount, 
                            'description': description})
    
    def check_funds(self, amount):
        return amount <= self.balance
    
    def _withdraw(self, amount, description=''):
        if not self.check_funds(amount):
            return False
        else:
            self.balance -= amount
            self.ledger.append({'amount': -amount,
                                'description': description})
            return True
        
    def withdraw(self, amount, description=''):
        if self._withdraw(amount, description):
            self.expenses += amount
            return True
        return False
        
    def get_balance(self):
        return self.balance
    
    def transfer(self, amount, destination):
        if not self._withdraw(amount, f'Transfer to {destination.name}'):
            return False
        else:
            destination.deposit(amount, f'Transfer from {self.name}')
            return True

def create_spend_chart(categories):
    print('Percentage spent by category')
    
    # Cálculo das percentagens
    percentage = {category.name: int((abs(category.expenses)*100/category.deposits)//10) for category in categories}
    
    # Barras do gráfico
    levels = [100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0]
    bars = [("o" * (int(percentage[cat.name]) + 1)).ljust(11)[::-1] for cat in categories]
    
    lines = []
    index = 0

    for level in levels:
        line = ''.join(f'{str(level).rjust(3)}| ' + '  '.join(bar[index] for bar in bars))
        lines.append(line)
        index += 1
            
    for l in lines:
        print(l)
    
    # Legenda
    names = [cat.name for cat in categories]
    max_length = max(len(name) for name in names)
    
    print(' ' * 3, '-' * (len(lines[0])-2))
    for index in range(max_length):
        print(' ' * 4, '  '.join(name[index] if index < len(name) else ' ' for name in names))

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:130.0) Gecko/20100101 Firefox/130.0

Challenge Information:

Build a Budget App Project - Build a Budget App Project

you should really return the output of the function

With a few modifications so that the output is returned instead of printed, it still doesn’t pass the test :confused:

class Category:
    # Atributos de classe
    def __init__(self, name):
        self.name = name
        self.balance = 0
        self.ledger = [ ]
        self.expenses = 0
        self.deposits = 0
    
    # Representação em string    
    def __str__(self):
        title = self.name.center(30, '*')
        registers = ''
        for register in self.ledger:
            description = register['description'][:23].ljust(23)
            amount = f'{register["amount"]:.2f}'.rjust(7)
            registers += f'{description}{amount}\n'
        total = f'Total: {self.balance}'
        return f'{title}\n{registers}{total}'
        
    def deposit(self, amount, description=''):
        self.balance += amount
        self.deposits += amount
        self.ledger.append({'amount': amount, 
                            'description': description})
    
    def check_funds(self, amount):
        return amount <= self.balance
    
    def _withdraw(self, amount, description=''):
        if not self.check_funds(amount):
            return False
        else:
            self.balance -= amount
            self.ledger.append({'amount': -amount,
                                'description': description})
            return True
        
    def withdraw(self, amount, description=''):
        if self._withdraw(amount, description):
            self.expenses += amount
            return True
        return False
        
    def get_balance(self):
        return self.balance
    
    def transfer(self, amount, destination):
        if not self._withdraw(amount, f'Transfer to {destination.name}'):
            return False
        else:
            destination.deposit(amount, f'Transfer from {self.name}')
            return True

def create_spend_chart(categories):
    title = 'Percentage spent by category'
    
    # Cálculo das percentagens
    percentage = {category.name: int((abs(category.expenses)*100/category.deposits)//10) for category in categories}
    
    # Barras do gráfico
    levels = [100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0]
    bars = [("o" * (int(percentage[cat.name]) + 1)).ljust(11)[::-1] for cat in categories]
    
    lines = []
    index = 0

    for level in levels:
        line = ''.join(f'{str(level).rjust(3)}| ' + '  '.join(bar[index] for bar in bars))
        lines.append(line)
        index += 1
    
    # Legenda
    names = [cat.name for cat in categories]
    max_length = max(len(name) for name in names)
    division = ' ' * 4 + '-' * (len(lines[0]) - 2)

    legend_lines = []
    for index in range(max_length):
        legend_lines.append(' ' * 5 + '  '.join(name[index] if index < len(name) else ' ' for name in names))
    
    # Output
    result = '\n'.join([title] + lines + [division] + legend_lines)
    return result

now that it returns a string, open the browser console when you run the tests to see extra output from the tests

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

I will probably be able to solve it now, thanks for the help!