Tell us what’s happening:
My code can’t seem to pass the “Printing a Category instance should give a different string representation of the object.” test, although I have added a str method which alters the Category instances. Help please!
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 = str(round(item['amount'], 2))[: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 create_spend_chart(categories):
pass
test = Category('test')
test1 = Category('test1')
test.deposit(1000, '012345678901234567890123456789')
print(test.withdraw(1500, 'test'))
test.transfer(200, test1)
print(test)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project