In your __str__ method, there’s actually an easier way of getting the descriptions and amounts. Try an enumerated for loop with ‘i’ and an additional term to get descriptions and amounts in the ledger. From there, you need only two variables – one for description, one for amount, and you can use a key to get what you need from the term you specified in the for loop header.
As for the title for the categories — there’s an easier way using string formatting as well.
**************************Food**************************initial deposit 1000.00
groceries -10.15
restaurant and more foo -15.89
Transfer to Clothing -50.00
Total: 923.96
@dhess
0 was just an example. I tried several numbers and they all gave an index error, it seems to have fixed itself though. Could you look at my code now and see if I missed anything?
Thanks.
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
def __str__(self):
length = len(self.name)
stars = int((4 - length) / 2)
title_1 = ("*" * stars) + self.name + ("*" * (4-length-stars))
for i, description in enumerate(self.ledger):
amounts = [self.ledger[i]['amount'] for i in range(len(self.ledger))]
descriptions = [self.ledger[i]['description'] for i in range(len(self.ledger))]
initial_deposit = descriptions[0][:23]
category_1 = descriptions[1][:23]
descrip_1 = descriptions[2][:23]
transfers = descriptions[3][:23]
balance = self.get_balance()
return f'*************{title_1}*************\n{initial_deposit:<23}{amounts[0]:>7.2f}\n{category_1:<23}{amounts[1]:>7.2f}\n{descrip_1:<23}{amounts[2]:>7.2f}\n{transfers:<23}{amounts[3]:>7.2f}\nTotal: {balance:.2f}'
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 transfer(self, amount, destination_category):
self.withdraw(amount, f"Transfer to {destination_category.name}")
destination_category.deposit(amount, f"Transfer from {self.name}")
if amount <= self.get_balance():
return True
else:
return False
def check_funds(self, amount):
if amount <= self.get_balance():
return True
else:
return False
def create_spend_chart(categories):
pass
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)
print(food)
It looks like you are forcing values in your str method. Try adding a deposit and withdraw for clothing and printing it. What do you see in the console?
I don’t know if it’s related to the issue you’re having, but you appear to be hardcoding amounts and descriptions in your str method’s return section. You don’t need to do that. You can return a single string with the title variable and the lists you made – amounts and descriptions.