Tell us what’s happening:
Hi!! everyone … let plz help me to fix my wrong code. just remain 1 statement to meet the goal. Thx
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 self.check_funds(amount):
self.ledger.append({"amount": -amount, "description": description})
return True
else:
return False
def get_balance(self):
balance = 0
for item in self.ledger:
balance += item['amount']
return balance
def check_funds(self, amount):
return amount <= self.get_balance()
def transfer(self, amount, category):
if self.withdraw(amount, f"Transfer to {category.name}"):
category.deposit(amount, f"Transfer from {self.name}")
return True
else:
return False
def __str__(self):
title = f"{self.name:*^30}\n"
items = ""
for item in self.ledger:
items += f"{item['description'][:23]:23}{item['amount']:>7.2f}\n"
total = f"Total: {self.get_balance():.2f}"
return title + items + total
def get_spent(self):
return sum(item['amount'] for item in self.ledger if item['amount'] < 0)
def create_spend_chart(categories):
# Calcula el porcentaje de gasto para cada categoría
total_spent = sum(category.get_spent() for category in categories)
category_percentages = [(category.name, category.get_spent() / total_spent * 50) for category in categories]
# Construye el encabezado del gráfico
chart = "Percentage spent by category\n"
# Construye las barras del gráfico
for i in range(100, -1, -10):
chart += f"{i:3}| "
for _, percent in category_percentages:
chart += "o " if percent >= i - 10 else " "
# chart += "o " if percent >= i else " "
chart += "\n"
# Construye la línea horizontal debajo de las barras
chart += " " + "-" * (len(categories) * 3 + 1) + "\n"
# Encuentra la longitud máxima de los nombres de las categorías
max_name_length = max(len(name) for name, _ in category_percentages)
# Construye los nombres de las categorías debajo del gráfico
for i in range(max_name_length):
chart += " "
for name, _ in category_percentages:
chart += name[i] + " " if i < len(name) else " "
chart += "\n"
return chart
# Ejemplo de uso
clothing_category = Category("Clothing")
auto_category = Category("Auto")
food_category = Category("Food")
food_category.deposit(100, "Initial deposit")
food_category.withdraw(20, "Groceries")
food_category.withdraw(30, "Restaurant")
categories = [food_category, clothing_category, auto_category]
print(create_spend_chart(categories))
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
Challenge Information:
Build a Budget App Project - Build a Budget App Project