class Category:
def \__init_\_(self, name):
self.name = name
self.ledger = \[\]
def deposit(self, amount, description = ''):
entry = {'amount': amount, 'description': description}
self.ledger.append(entry)
def withdraw(self, amount, description = ''):
if self.check_funds(amount):
entry = {'amount': -amount, 'description': description}
self.ledger.append(entry)
return True
return False
def get_balance(self):
balance = 0
for item in self.ledger:
balance += item\['amount'\]
return 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
return False
def check_funds(self, amount):
if self.get_balance() >= amount:
return True
return False
def \__str_\_(self):
final = self.name.center(30, '\*') + '\\n'
total = 0
for item in self.ledger:
desc = item\['description'\]\[0:23\]
amt = f"{item\['amount'\]:.2f}"
final += f"{desc:<23}{amt:>7}\\n"
total += item\['amount'\]
return final + f"Total: {total}"
food = Category(‘Food’)
food.deposit(1000, ‘initial deposit’)
food.withdraw(10.15, ‘groceries’)
food.withdraw(15.89, ‘restaurant’)
clothing = Category(‘Clothing’)
food.transfer(200, clothing)
#clothing.withdraw(100, ‘shirt’)
#print(food)
#print(clothing)
total = 0
def create_spend_chart(categories):
for category in categories:
for item in category.ledger:
amt = item\['amount'\]
if amt < 0:
global total
total += abs(item\['amount'\])
print ('Percentage spent by category')
percentages = \[\]
for category in categories:
spent = 0
for item in category.ledger:
spent += (abs(item\['amount'\]) if item\['amount'\] < 0 else 0)
percent = (spent / total \* 100) // 10 \* 10
percentages.append(percent)
for num in range(100, -1, -10):
print(f"{num:>3}|", end='')
for percent in percentages:
if percent >= num:
print(" o ", end="")
else:
print(" ", end="")
print()
last_line = len(percentages)
print(' ' + '---' \* last_line + '-')
category_list = \[\]
for category in categories:
category_list.append(category.name)
longest = max(category_list, key = len)
for i in range(len(longest)):
print(' ', end='')
for category in category_list:
if len(category) > i:
print(category\[i\], end=" ")
else:
print(' ', end='')
print()
create_spend_chart([food,clothing])
Where is the issue in this code i can’t resolve it. Please help im doing this for hours
