Tell us what’s happening:
I see no difference with my output and what is asked by the challenge, I can’t get rid of the last test Fail
Your code so far
class Category:
def __init__(self, cat):
self.cat = cat
self.ledger = []
self.balance = 0
self.sum_exp = 0
def __repr__(self):
return self.cat
def __str__(self):
l_cat = len(self.cat)
self.header = format(self.cat, "*^30") + '\n'
self.body = ''
for i in range(len(self.ledger)):
self.body += format(str(self.ledger[i]['description'])[:23],'<23') + str(format(self.ledger[i]['amount'],'>7.2f')) + '\n'
self.footer = 'Total: ' + format(self.balance,'.2f')
return (self.header+self.body+self.footer)
def get_balance(self):
self.balance = 0
for i in range(len(self.ledger)):
self.balance += self.ledger[i]['amount']
return(self.balance)
def check_funds(self, amount):
if amount<=self.balance:
return True
else:
return False
#used in withdraw and transfer
def deposit(self, amount=0, description=""):
item = {"amount": amount, "description": description}
self.ledger.append(item)
self.balance += item['amount']
# print(self.ledger)
def withdraw(self, amount=0, description=""):
item = {"amount": amount*(-1), "description": description}
if self.check_funds(amount):
self.ledger.append(item)
self.balance += item['amount']
# print(self.ledger)
return True
else:
return False
def transfer(self, amount, dest_cat):
from_cat = format(str(self.cat),"s")
if self.check_funds(amount):
self.withdraw(amount, "Transfer to "+repr(dest_cat))
dest_cat.deposit(amount, "Transfer from "+from_cat)
return self.check_funds(amount)
def get_expenses(self):
self.sum_exp = 0
for i in range(len(self.ledger)):
if self.ledger[i]['amount']<0 :
self.sum_exp += self.ledger[i]['amount']
return(self.sum_exp)
def create_spend_chart(lista):
expenses = {}
exp_quota = {}
quota = {}
total_exp = 0
for cat in lista:
total_exp += cat.get_expenses()
for cat in lista:
expenses[cat.cat] = cat.get_expenses()
exp_quota[cat.cat] = "{0:.0%}".format(((abs(cat.get_expenses()/total_exp)*100)//10)/10)
quota[cat.cat] = int(abs(cat.get_expenses()/total_exp)*100//10*10)
#print(quota)
#--------------------------------------creo colonna side
side = []
for i in range(11):
side.append(str(format(100-i*10,'>3'))+"| ")
#--------------------------------------creo colonne categoria
cats = []
for cat in lista:
bins = []
for i in range(11):
if i*10 >= 100-quota[cat.cat]:
bins.append("o ")
else:
bins.append(" ")
cats.append(bins)
#--------------------------------------grafico
lines = []
print("Percentage spent by category")
for i in range(11):
string = ""
for j in range(len(lista)):
string += cats[j][i]
lines.append(side[i]+string)
# lines.append(side[i]+cats[0][i]+cats[1][i]+cats[2][i])
print(lines[i])
#--------------------------------------creo asse x
dashline =" "*4 + (len(lista)*3+1)*"-"
print(dashline)
#--------------------------------------descrizioni categorie
desc_list = []
max_len_desc = 0
for cat in lista:
if max_len_desc < len(cat.cat):
max_len_desc = len(cat.cat)
for cat in lista:
desc = []
cat_len = 0
for char in cat.cat:
desc.append(char)
cat_len+=1
while cat_len < max_len_desc:
desc.append(" ")
cat_len+=1
desc_list.append(desc)
desc_lines = []
for i in range(max_len_desc):
string = ""
for j in range(len(lista)):
string += desc_list[j][i]+" "
desc_lines.append(" "*5 + string)
# desc_lines.append(" "*5 + desc_list[0][i]+" "+desc_list[1][i]+" "+desc_list[2][i]+" ")
print(desc_lines[i])
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0
.
Challenge: Budget App
Link to the challenge: