Tell us what’s happening:
I have no idea why the function is not working, I tried everything, remade the entire code 3 times, look for similar mistakes in the forum, but nothing. I’m sorry some words are in spanish, but again, 3rd time writing it.
It’s probably something silly I’m not seeing, but it executes correctly in my opinion.
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 not self.check_funds(amount):
return False
self.deposit(-amount, description)
return True
def transfer (self, amount, category):
if not self.check_funds(amount):
return False
category.deposit(amount, f'Transfer from {self.__name}')
self.withdraw(amount, f'Transfer to {category.__name}')
return True
def get_balance (self):
summatory = 0
for element in self.ledger:
summatory += element['amount']
return summatory
def check_funds (self, amount):
return self.get_balance() >= amount
def getLedger(self):
return self.ledger
def __str__ (self):
text = f"{self.__name:*^30}\n"
for element in self.ledger:
nombre = f"{element['description'][:23]}"
numero = f"{element['amount']:>7.2f}"
text += f"{nombre:<23}{numero:>7}\n"
text += f'Total: {self.get_balance()}'
return text
def create_spend_chart(categories):
gastadoFIN = 0
elementos = []
totalGastados = []
porcentajes = []
text = 'Percentage spent by category'
text += '\n'
for gasto in categories.getLedger():
gastado = 0
if gasto["amount"] < 0:
gastado += -gasto["amount"]
totalGastados.append(gastado)
elementos.append(gasto['description'])
gastadoFIN += gastado
for gasto in totalGastados:
porcentaje = (gasto / gastadoFIN) * 100
porcentajes.append(porcentaje)
for linea in range(100, -1, -10):
text += f'{linea:>3} | '
for porcentaje in porcentajes:
text += "o " if porcentaje >= linea else " "
text += '\n'
text += " -" + "---"*len(elementos) + '\n'
largo_maximo = max(len(e) for e in elementos)
for indice in range(largo_maximo):
text += ' '
for elemento in elementos:
if indice < len(elemento):
text += elemento[indice] + ' '
else:
text += " "
text += '\n'
return text
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(food)
print('\n')
print(create_spend_chart(food))
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:143.0) Gecko/20100101 Firefox/143.0
Challenge Information:
Build a Budget App Project - Build a Budget App Project
