Tell us what’s happening:
Hello. I hope someone could point to me the discrepancies on my code. I have compared the spacings with the others with the same problem .
I have this error info: “Diff is 1251 characters long.”
Your code so far
def create_spend_chart(categories):
cat_list = []
all_spent = {}
sum_all_spent = 0
for x in categories:
sum_all_spent += x.all_spent()* -1
all_spent[x.category] = x.all_spent()* -1
for x in all_spent:
i= all_spent[x]
val= (i/sum_all_spent)*100
rounded = int(math.floor(val / 10.0)) * 10
all_spent[x] = rounded
for x in categories:
cat_list.append(list(x.category))
max = 0
for x in cat_list:
if max < len(x):
max = len(x)
for x in cat_list:
if len(x) < max:
diff = max - len(x)
x += [' ']* diff
bar = []
header = 'Percentage spent by category'
bar.append(header+'\n')
for ct in range(100, -10, -10):
bar.append((str(ct)+'|').rjust(4))
for x in all_spent:
# print(all_spent[x])
if all_spent[x] >= ct:
bar.append(' o ')
else:
bar.append(' ')
bar.append('\n')
bar.append(' '+'-'*(len(categories)*3+1)+'\n')
all_cat = list([[*a] for a in zip(*cat_list)])
if len(cat_list) == 3:
for x in all_cat:
bar.append(' '+str(x[0]+' '+x[1]+' '+x[2])+'\n')
elif len(cat_list) == 4:
for x in all_cat:
bar.append(' '+str(x[0]+' '+x[1]+' '+x[2]+' '+x[3])+'\n')
chart = ''.join([i for i in bar])
return chart
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36.
I having the same problem here. How does setting maxDiff solve your problem . The problem still exists or at least my problem. "Diff is 902 characters long.”
Which characters are different ?
class Category:
instances = []
def __init__(self,name=False):
self.name=name
self.ledger=list()
self.__class__.instances.append(self.name)
#self.name=[[] for _ in range(4)] #Creates a list, that contains 4 listsclass Category:
instances = []
def __init__(self,name=False):
self.name=name
self.ledger=list()
self.__class__.instances.append(self.name)
#self.name=[[] for _ in range(4)] #Creates a list, that contains 4 lists
def check_funds(self,amount):
amt=0
#len_ledger=len(self.ledger)
for i in range(len(self.ledger)):
amt=amt+self.ledger[i]["amount"]
if amt<amount:
return False
else:
return True
def deposit(self,amount,description=""):
self.dep=dict()
#adding the amount and description to dictionary
self.dep["amount"]=amount
self.dep["description"]=description
#adding the deposit to ledger list
return(self.ledger.append(self.dep))
def withdraw(self,amount,description=""):
#checking if total amount less than or greaten than amount to be withdrawn
l=self.check_funds(amount)
if(l==True):
self.withd=dict()
self.withd["amount"]=-(amount)
self.withd["description"]=description
self.ledger.append(self.withd)
return True
else:
return False
def total_with(self):
tot_with = 0
n = len(self.ledger)
for i in range(n):
desc = self.ledger[i]["description"]
if self.ledger[i]["amount"] < 0 and not(desc.startswith('Transfer')):
tot_with+=self.ledger[i]["amount"]
return tot_with
def get_balance(self):
fund=0
n=len(self.ledger)
#retrieving the total fund in ledger
for i in range(n):
fund=fund+self.ledger[i]["amount"]
return fund
def transfer(self,amount,obname):
objectname=obname.name
a=self.withdraw(amount,f"Transfer to {objectname}")
b=obname.deposit(amount,f"Transfer from {self.name}")
if(a==True):
return True
else:
return False
def create_spend_data(self):
tot_with = 0
total = 0
for i in range(len(self.ledger)):
if self.ledger[i]['amount'] < 0:
tot_with += self.ledger[i]['amount']
total += self.ledger[i]['amount']
return(-tot_with/total)
def __str__(self):
title = f"{self.name:*^30}\n"
items = ""
total = 0
for i in range(len(self.ledger)):
items += f"{self.ledger[i]['description'][0:23]:23}" + f"{self.ledger[i]['amount']:>7.2f}" + '\n'
total += self.ledger[i]['amount']
output = title + items + "Total: " + str(total)
return(output)
@classmethod
def printInstances(cls):
t = []
for instance in cls.instances:
t.append(instance)
return(t)
def create_spend_chart(category):
count = 0
d = {} #Empty dictionary to add values into
list_dict = list(category)
length = len(category)
total_spent = 0
for i in category:
total_spent = i.total_with() + total_spent
os = ''
for i in category:
t = (10 - (int(abs(i.total_with()/total_spent)*10))) * ' ' + \
(int(abs(i.total_with()/total_spent)*10)+1) * 'o'
os = t
d[count] = os
count+=1
max_len = max(len(category[0].name),len(category[1].name),len(category[2].name))
cat1 = category[0].name + (max_len - len(category[0].name)) * ' '
cat2 = category[1].name + (max_len - len(category[1].name)) * ' '
cat3 = category[2].name + (max_len - len(category[2].name)) * ' '
temp_text =''
for t in range(max_len):
if t == max_len-1:
temp = ' '+cat1[max_len-1-t].upper()+' '+cat2[max_len-1-t].upper()+' '+cat3[max_len-1-t].upper()+' \n'
temp_text = temp + temp_text
else:
temp = ' '+cat1[max_len-1-t]+' '+cat2[max_len-1-t]+' '+cat3[max_len-1-t]+' \n'
temp_text = temp + temp_text
temp_text = ' ----------\n' + temp_text
spend_chart = ''
temp_text2 = ''
temp = 'Percentage spent by category\n'
for v in range(11):
if v == 0:
temp_text2 = str(100-(v*10)) + '| '+d[0][v]+' '+d[1][v]+' '+d[2][v]+' \n'
elif v==10:
temp_text2 = temp_text2 + ' '+ str(100-(v*10)) + '| '+d[0][v]+' '+d[1][v]+' '+d[2][v]+' \n'
else:
temp_text2 = temp_text2 + ' '+ str(100-(v*10)) + '| '+d[0][v]+' '+d[1][v]+' '+d[2][v]+' \n'
spend_chart = temp + temp_text2 + temp_text
spend_chart = temp + temp_text2 + temp_text
return(spend_chart)
it shows you a comparison of the expected result(spacing) and yours. it will show a ++ or – or ? on the lines in question.
if it’s the right spacing, you will see only the desired result. check the snapshots. the left image shows that there are still errors on the spacing. the right one is corrected already.