Tell us what’s happening:
I am currently failing the second to last test (checking if the string output is acceptable). The output looks the same to me though. Does anyone see something I do not?
Your code so far
class Category:
def __init__(self, name):
self.ledger = []
self.name = name
def _strhelp(self, string, amount):
amount = f'{amount:.2f}'
if len(string) + len(amount) > 29:
spacenum = ' '
string = string[0:29 - len(amount)]
else:
spacenum = ' '*(30 - (len(string) + len(amount)))
final_str = f'{string}{spacenum}{float(amount):.2f}'
return(final_str)
def __str__(self):
title = self.name.center(30, '*')
initial = self._strhelp('initial deposit', self.ledger[0]['amount'] )
trans = ''
for i in range(len(self.ledger) - 1):
trans += self._strhelp(self.ledger[i+1]['description'], self.ledger[i+1]['amount'])
trans += '\n'
total = f'Total: {self.get_balance()}'
final_str = title + '\n' + initial + '\n' + trans + total
return final_str
def check_funds(self, amount):
if self.get_balance() < amount:
return False
else:
return True
def deposit(self, amount, desc=''):
self.ledger.append({'amount': amount, 'description': desc})
def withdraw(self, amount, desc=''):
if not self.check_funds(amount):
return False
self.ledger.append({'amount': -amount, 'description': desc})
return True
def get_balance(self):
total = sum([self.ledger[i]['amount'] for i in range(len(self.ledger))])
return total
def transfer(self, amount, category):
if not self.check_funds(amount):
return False
self.withdraw(amount, f'Transfer to {category.name}')
category.deposit(amount, f'Transfer from {self.name}')
return True
def create_spend_chart(categories):
pass
food = Category('Food')
food.deposit(1000, '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)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project