Hey guys,
I have been working on the Budget app and I am stuck for some time now. I want to get the {amount} and {description} from the self.ledger and put them on a new string inside the repr function. That way, when the user calls print(category) it displays according to the challenge’s requirement.
Here’s my code so far:
class category:
def __init__(self,name):
self.name = name
self.ledger = []
self.amounts =[]
def __repr__(self):
n = int((30 - len(self.name))/2)
num_asterisks = '*' * n
Headline = num_asterisks+self.name+num_asterisks+'\n'
for i in self.ledger:
description_index = i.index(description)
transaction = i[description_index:]
def deposit (self, amount, description=''):
self.amounts.append(amount)
self.ledger.append(f'amount:{amount}, description:{description}')
return True
def withdraw (self, amount, description=''):
if self.check_funds(amount):
self.ledger.append(f'amount:{abs(amount)}, description:{description}')
self.amounts.append(amount*-1)
return True
else:
return False
def get_balance(self):
balance = sum(self.amounts)
return balance
def transfer (self, amount, other_category):
if self.check_funds(amount):
self.withdraw(amount, 'Transfer to'+ other_category.name)
other_category.deposit(amount,'Transfer from'+self.name)
return True
else:
return False
def check_funds(self,amount):
if amount > self.get_balance():
return False
else:
return True