Tell us what’s happening:
Hi, tests 17-24 are not passing. Although when checking and running the code, everything is fine and beautiful, it works with different amounts of spending and sums. What could be the problem?
Your code so far
class Category:
def __init__(self,name):
self.name = name
self.ledger = []
def deposit(self,amount,description=''):
self.ledger.append({'amount': amount, 'description': description})
def withdraw(self,amount,description=''):
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': description})
return True
else:
return False
def get_balance(self):
total = 0
for elem in self.ledger:
total+=elem['amount']
return total
def transfer(self,amount,destination):
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': f'Transfer to {destination.name}'})
destination.ledger.append({'amount': amount, 'description': f'Transfer from {self.name}'})
return True
else:
return False
def check_funds(self, amount):
if self.ledger[0]['amount']>=amount:
return True
else:
return False
def __str__(self):
lines=[('*'*((30-len(self.name))//2))+self.name+('*'*((30-len(self.name))//2))]
for obj in self.ledger:
desc = obj['description'][:23]
amo = '{:.2f}'.format(obj['amount'],2)
lines.append(f"{desc.ljust(23)}{str(amo).rjust(7)}")
lines.append(f'Total: {self.get_balance()}')
return '\n'.join(lines)
def create_spend_chart(categories):
gisto = ['Percentage spent by category']
categ=[]
dlin = 0
for obj in categories.ledger[1:]:
categ.append(obj['description'])
if len(obj['description'])>dlin:
dlin = len(obj['description'])
vsego_potrach = sum((-categories.ledger[1:][i]['amount']) for i in range (len(categ)))
znachen = {categ[i]:(-categories.ledger[1:][i]['amount']*100/vsego_potrach//10*10) for i in range(len(categ))}
dlina_strok = len(categ)*3+1
for i in range(100,-1,-10):
stroka=(str(i)+'|').rjust(4)
for key, elem in znachen.items():
if elem>=i:
stroka+=' o '
else:
stroka+=' '
gisto.append(stroka+' ')
gisto.append(('-'*dlina_strok).rjust(dlina_strok+4))
for i in range(dlin):
elem=' '
for j in range(len(categ)):
try:
elem+=' '+categ[j][i]
except IndexError:
elem+=' '
gisto.append(elem+' ')
result = '\n'.join(gisto)
return result
a=Category('Gabi')
a.deposit(900,'vsego')
a.withdraw(45.67,'Games')
a.withdraw(80,'Boards')
a.withdraw(150,'Product')
a.withdraw(50,'Apteka')
a.get_balance()
b = Category('Clothing')
print(create_spend_chart(a))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App - Build a Budget App