Tell us what’s happening:
i’ve been working on this certification going on 3 weeks now which is crazy. i can’t figure out why i keep getting caught up. it looks like its printing all of the categories added up as a float, i tried to get a sum() too, but it said its not iterable, i dont know how to separate it by the category. the math for percentages is simple enough to do, but i dont see where i can write the code yet and i can re-use the code from the rpg character maker to show the graph, but im stuck standing,
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 transaction in self.ledger:
balance += transaction['amount']
return balance
def check_funds(self, amount):
return amount <= self.get_balance()
def transfer(self, amount, destination_category):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {destination_category.name}")
destination_category.deposit(amount, f"Transfer from {self.name}")
return True
else:
return False
def __str__(self):
method = '\n'.join(f"{transaction['description']:23.23}{transaction['amount']:>7.2f}" for transaction in self.ledger )
return f"{self.name.center(30,'*')}\n{method}\nTotal: {self.get_balance()}"
def spreadsheet(*categories):
for category in categories:
print(category)
print()
def create_spend_chart(*categories):
print('Percentage spent by category\n')
total_withdraw = 0
for category in categories:
for transaction in category.ledger:
if transaction['amount'] < 0:
total_withdraw += transaction['amount']
print(total_withdraw)
food = Category('Food')
food.deposit(1000, 'initial deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
clothing.deposit(1000)
clothing.withdraw(100)
clothing.withdraw(30.14)
spreadsheet(food, clothing)
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/149.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App - Build a Budget App