Tell us what’s happening:
- The transfer method should create a specific ledger item in the category object passed as its argument.
- Printing a Category instance should give a different string representation of the object.
- create_spend_chart should print a different chart representation. Check that all spacing is exact. Open your browser console with F12 for more details.
My code keeps failing these three tests but im not sure why or whats going wrong.
Your code so far
class Category():
def __init__(self, name):
self.name = name
self.ledger = []
self.total = 0.0
def deposit(self, amount, description = ""):
self.ledger.append({'amount': amount, 'description': description})
self.total += amount
def withdraw(self, amount, description = ""):
if not self.check_funds(amount):
return False
else:
self.total += (amount*-1)
self.ledger.append({'amount': round(amount*-1,2), 'description': description})
return True
def get_balance(self):
return self.total
def transfer(self, amount, budget):
if self.withdraw(amount,f'Transfer to {budget.name}'):
budget.deposit(amount,f'Transfer from {budget.name}')
return True
else:
return False
def check_funds(self, amount):
if amount > self.total:
return False
else:
return True
def __repr__(self):
outputstring = ''
outputstring += f"{self.name:*^30}\n"
end = 23
final = 0.0
for t in self.ledger:
if len(t['description']) > 23:
outputstring += f"{t['description'][0:end]}{t['amount']:>{30 - len(t['description'][0:end])}}\n"
elif type(t['amount']) == type(int()):
a = f"{t['amount']:.2f}"
outputstring += f"{t['description'][0:end]}{a:>{30 - len(t['description'][0:end])}}\n"
else:
outputstring += f"{t['description']}{t['amount']:>{30 - len(t['description'])}}\n"
final += t['amount']
outputstring += f"Total: {final:.2f}"
return outputstring
food = Category('Food')
entertainment = Category('Entertainment')
food.deposit(1000, 'deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.79, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
entertainment.deposit(1000, 'ye')
entertainment.withdraw(60,'i said so')
clothing.withdraw(20,'lol')
#print(food.get_balance())
#print(str(food))
def create_spend_chart(categories):
total_withdraw = 0
catl = {}
chartstr = f"Percentage spent by category\n"
for c in categories:
ctotal = 0
for i in c.ledger:
if i['amount'] < 0:
total_withdraw += i['amount']
ctotal += abs(i['amount'])
catl[c.name] = ctotal
total_withdraw = abs(total_withdraw)
for key in catl:
percentage = (catl[key]/total_withdraw)*100
catl[key] = percentage
for p in range(100,-10,-10):
if p == 100:
chartstr += f"{str(p) + '|'}"
elif p == 0:
chartstr += f" {str(p) + '|'}"
else:
chartstr += f" {str(p) + '|'}"
for value in catl.values():
if p <= value:
chartstr += f' o '
chartstr += '\n'
L = len(catl.items())
chartstr += " "f"{'-'*(L*3 + 1)}"
chartstr += "\n"
categorylength = 0
for i in categories:
if len(i.name) > categorylength:
categorylength = len(i.name)
for i in range(categorylength):
chartstr += " "
for n in categories:
if i < len(n.name):
chartstr += ' ' + n.name[i] + ' '
else:
chartstr += " "
chartstr += " \n"
print(chartstr)
return chartstr
create_spend_chart([food, entertainment, clothing])
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project