Tell us what’s happening:
My brain is melting, Ive been at this for 4 hours straight. I can not for the life of me figure out what Im doing wrong. Im going to bed and will check up on this post tomorrow. Thanks for any help/pointers.
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 not self.check_funds(amount):
return False
else:
self.ledger.append({'amount': -amount, 'description': description})
return True
def get_balance(self):
return sum([entry['amount'] for entry in self.ledger])
def check_funds(self, amount):
if amount > self.get_balance():
return False
else:
return True
def transfer(self, amount, category):
if not self.check_funds(amount):
return False
else:
self.withdraw(amount, "Transfer to " + category.name)
category.deposit(amount, "Transfer from " + self.name)
return True
def __str__(self):
output = self.name.center(30, "*")
for entry in self.ledger:
output += "\n" + entry['description'][:23].ljust(23) + f"{entry['amount']:.2f}".rjust(7)
output += "\nTotal: " + f"{self.get_balance():.2f}"
return output
def create_spend_chart(categories):
spent = [abs(sum([entry['amount'] for entry in category.ledger if entry['amount'] < 0])) for category in categories]
total = sum(spent)
percentages = [int((amount / total) * 100) // 10 * 10 for amount in spent]
chart = "Percentage spent by category\n"
for i in range(100, -1, -10):
chart += str(i).rjust(3) + "|"
for p in percentages:
if p >= i:
chart += " o"
else:
chart += " "
chart += "\n"
chart += " " + "-" * (len(categories) * 3 + 1) + "\n"
max_len = max(len(category.name) for category in categories)
for l in range(0, max_len):
chart += " "
for category in categories:
if l < len(category.name):
chart += " " + category.name[l]
else:
chart += " "
chart += " \n"
print(chart)
return chart.rstrip("\n")
food = Category("Food")
food.deposit(1000, "initial deposit")
food.withdraw(10.15, "groceries")
clothing = Category("Clothing")
food.transfer(50, clothing)
print(create_spend_chart([food, clothing]))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App - Build a Budget App