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