Tell us what’s happening:
I’m not quite sure what I’m doing wrong, any help is appreciated!
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
def __str__(self):
final_string = self.name.center(30, '*')
for item in self.ledger:
description_string = str(item['description'])[:23].ljust(23)
amount_string = "{:.2f}".format(item['amount'])[:7].rjust(7)
final_string += '\n' + description_string + amount_string
return final_string + '\n' + 'Total: ' + str(self.get_balance()) + ''
def deposit(self, amount, description=''):
self.ledger.append({'amount': amount, 'description': description})
def withdraw(self, amount, description=''):
if self.get_balance() > abs(amount):
self.ledger.append({'amount': -amount, 'description': description})
return True
else:
return False
def get_balance(self):
balance = 0
for expense in self.ledger:
balance += expense['amount']
return balance
def transfer(self, amount, category):
if self.check_funds(amount):
self.withdraw(amount, f'Transfer to {category.name}')
category.deposit(amount, f'Transfer from {self.name}')
return True
else:
return False
def check_funds(self, amount):
budget = 10
for item in self.ledger:
budget += item['amount']
return budget > amount
def get_withdrawals(self):
return sum(item["amount"] for item in self.ledger if item["amount"] < 0)
def create_spend_chart(categories):
total_withdrawals = sum(category.get_withdrawals() for category in categories)
percentages = [(category.get_withdrawals() / total_withdrawals) * 100 for category in categories]
chart = "Percentage spent by category\n"
for i in range(100, -1, -10):
chart += str(i).rjust(3) + "| "
for percentage in percentages:
if percentage >= i:
chart += "o "
else:
chart += " "
chart += "\n"
chart += " " + "-" * (len(categories) * 3 + 1) + "\n"
max_name_length = max(len(category.name) for category in categories)
category_names = [category.name.ljust(max_name_length) for category in categories]
for i in range(max_name_length):
chart += " "
for name in category_names:
chart += name[i] + " "
chart += "\n"
return chart
test = Category('test')
test1 = Category('test1')
test2 = Category('test2')
test3 = Category('test3')
test.deposit(1000, 'sad')
test.withdraw(500, 'test')
test.transfer(200, test1)
categories_list = [test, test1, test2, test3]
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project