Tell us what’s happening:
El proyecto Proyecto de creación de una aplicación de presupuesto, el metodo create_spend_chart, pide crear un gráfico, con las mismas dimensiones pero diferente al del ejemplo, lo he hecho de forma vertical, horizontal y me sigue fallando el último punto con el siguiente error: " create_spend_chart Debe imprimir una representación gráfica diferente. Verifique que el espaciado sea exacto." ayuda, es el último punto que me falta para la cerificación.
Your code so far
class Category:
def __init__(self, nombre):
self.nombre = nombre
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, 'description': description})
return True
return False
def get_balance(self):
return sum(item['amount'] for item in self.ledger)
def transfer(self, amount, other_category):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {other_category.nombre}")
other_category.deposit(amount, f"Transfer from {self.nombre}")
return True
return False
def check_funds(self, amount):
return amount <= self.get_balance()
def __str__(self):
title = self.nombre.center(30, '*')
items = [f"{item['description'][:23].ljust(23)}{item['amount']:7.2f}" for item in self.ledger]
total = f"Total: {self.get_balance():.2f}"
return '\n'.join([title] + items + [total])
def create_spend_chart(categories):
# Calcular el total gastado en cada categoría
total_spent = {}
for category in categories:
spent = sum(-item['amount'] for item in category.ledger if item['amount'] < 0)
total_spent[category.nombre] = spent
total_expenses = sum(total_spent.values())
# Calcular el porcentaje gastado en cada categoría
if total_expenses == 0:
percentages = {cat: 0 for cat in total_spent}
else:
percentages = {cat: (spent / total_expenses) * 100 for cat, spent in total_spent.items()}
# Construir el gráfico
chart = ["Percentage spent by category"]
# Generar las líneas del gráfico
for i in range(100, -1, -10):
line = f"{i:3}| "
for cat in categories:
if percentages[cat.nombre] >= i:
line += "o "
else:
line += " "
chart.append(line.rstrip())
chart.append(" " + "-" * (len(categories) * 3 + 1))
# Añadir nombres de categorías en la parte inferior
max_name_length = max(len(cat.nombre) for cat in categories)
for i in range(max_name_length):
line = " "
for cat in categories:
name = cat.nombre
if i < len(name):
line += f"{name[i]} "
else:
line += " "
chart.append(line.rstrip())
return '\n'.join(chart)
# Crear categorías
food = Category('Food')
food.deposit(150, 'initial deposit')
food.withdraw(100.15, 'groceries')
food.withdraw(250.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
clothing.deposit(50, 'initial deposit')
clothing.withdraw(35.55, 'clothes')
clothing.withdraw(85.25, 'shoes')
auto = Category('Auto')
auto.deposit(500, 'initial deposit')
auto.withdraw(100, 'Tires')
auto.withdraw(150, 'paint')
# Transferir dinero
food.transfer(50, clothing)
# Imprimir las categorías
print(food)
print(clothing)
# Crear gráfico de gastos
print(create_spend_chart([food, clothing,auto]))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project