Tell us what’s happening:
The result is already the same as example, but it still error on create_spend_chart. I have already compared with this test : ( taken from freecodecamp github )
def test_create_spend_chart(self):
self.food.deposit(900, “deposit”)
self.entertainment.deposit(900, “deposit”)
self.business.deposit(900, “deposit”)
self.food.withdraw(105.55)
self.entertainment.withdraw(33.40)
self.business.withdraw(10.99)
actual = budget.create_spend_chart([self.business, self.food, self.entertainment])
expected = "Percentage spent by category\n100| \n 90| \n 80| \n 70| o \n 60| o \n 50| o \n 40| o \n 30| o \n 20| o o \n 10| o o \n 0| o o o \n ----------\n B F E \n u o n \n s o t \n i d e \n n r \n e t \n s a \n s i \n n \n m \n e \n n \n t "
self.assertEqual(actual, expected, ‘Expected different chart representation. Check that all spacing is exact.’)
`);
Please help.
The replit link is : budget-app - Replit
Your code so far
class Category:
def __init__(self,category):
self.category = category
self.balance = 0
self.ledger = []
def deposit(self,dep=0,desc=""):
if dep > 0 :
depo = {}
# depo["type"] ='d'
depo["amount"] = dep
depo["description"] = desc
self.balance += dep
self.ledger.append(depo)
def withdraw(self,damount=0,description=""):
if self._check_funds(damount) :
draw = {}
self.balance -= damount
# draw["type"] = 'w'
draw["amount"] = -damount
draw["description"] = description
self.ledger.append(draw)
else :
return False
return True
def transfer(self,tamount,cat):
if self._check_funds(tamount) :
draw = {}
# draw["type"] = 'w'
draw["amount"] = -tamount
draw["description"] = "Transfer to " + cat.category
self.ledger.append(draw)
self.balance -= tamount
cat.deposit(tamount,"Transfer from " + self.category)
else:
return False
return True
def _check_funds(self,amount):
if self.balance >= amount:
return True
else:
return False
def check_funds(self,amount):
return self._check_funds(amount)
def get_balance(self):
return self.balance
def __str__(self):
title = f"{self.category:*^30}\n"
items = ""
for item in self.ledger:
items += f"{item['description'][:23]:<23}" + f"{item['amount']:>7.2f}\n"
result = title + items + f"Total: {self.balance:.2f}"
return result
def create_spend_chart(categories):
diag = []
tspent = 0
cspent = []
clen =0
result = "Percentage spent by category\n"
for i in range(100,-1,-10):
diag.append([i,'|'])
for c in categories:
tspent += c.total_spent()
for c in categories:
pspent = (c.total_spent()/tspent)*100
clen = len(c.category) if clen < len(c.category) else clen
pspent = (pspent//10)*10
cspent.append([c.category.capitalize(),pspent])
for i in diag:
i[1] = f"{i[0]: >3}{i[1]}"
for j in cspent:
if i[0] <= j[1]:
i[1] += ' o '
else:
i[1] += ' '
result += i[1]+' \n'
line = f"{' '*4}"
for c in range(len(cspent)):
line += f"{'-'*3}"
line += '-'
for c in cspent:
c[0] += ' '*(clen-len(c[0]))
result += line + '\n'
# print(cspent)
line = ""
for i in range(clen):
line +=f"{' '*4}"
for j in cspent:
line+=(f" {j[0][i]} ")
if i < clen-1 : line += ' \n'
else: line += ' '
result +=line
return(result)
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Challenge Information:
Scientific Computing with Python Projects - Budget App