I dont know what is wrong
someone help me please
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.ledger.append({"amount": -amount, "description": description})
return True
def get_balance(self):
return sum(item["amount"] for item in self.ledger)
def transfer(self, amount, other_category):
if not self.check_funds(amount):
return False
if not self.withdraw(amount, f"Transfer to {other_category.name}"):
return False
other_category.deposit(amount, f"Transfer from {self.name}")
return True
def check_funds(self, amount):
return amount <= self.get_balance()
def __str__(self):
title = self.name.center(30, "*")
items = ""
for item in self.ledger:
description = item["description"][:23].ljust(23)
amount = f"{item['amount']:.2f}".rjust(7)
items += description + amount + "\n"
total = self.get_balance()
return title + "\n" + items + "Total: " + f"{total:.2f}"
def create_spend_chart(categories):
total_withdrawals = 0
category_withdrawals = {}
for category in categories:
category_withdrawals[category.name] = 0
for item in category.ledger:
if item["amount"] < 0:
category_withdrawals[category.name] += abs(item["amount"])
total_withdrawals += abs(item["amount"])
if total_withdrawals == 0:
return "Percentage spent by category\nNo withdrawals to display"
chart = "Percentage spent by category\n"
percentages = {}
for category_name, withdrawals in category_withdrawals.items():
percentages[category_name] = int((withdrawals / total_withdrawals) * 100)
for i in range(100, -1, -10):
chart += f"{i:3}| "
for category_name in category_withdrawals:
if percentages[category_name] >= i:
chart += "o "
else:
chart += " "
chart += "\n"
chart += " " + ("---" * len(categories)) + "-\n"
max_name_length = max(len(category.name) for category in categories)
for i in range(max_name_length):
chart += " "
for category in categories:
if i < len(category.name):
chart += category.name[i] + " "
else:
chart += " "
chart += "\n"
return chart