Hello ,
Could you clue me into what is incorrect spacing with my spend chart ?
Thank you.
FAIL: test_create_spend_chart (test_module.UnitTests)
Traceback (most recent call last):
File “/home/runner/PhonyVapidCodewarrior/test_module.py”, line 94, in test_create_spend_chart
self.assertEqual(actual, expected, ‘Expected different chart representation. Check that all spacing is exact.’)
AssertionError: 1 != 'Percentage spent by category\n100| [384 chars] t ’ : Expected different chart representation. Check that all spacing is exact.
Ran 11 tests in 0.002s
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 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 check_funds(self,amount):
fund=0
n=len(self.ledger)
print(n)
for i in range(n):
fund=fund+self.ledger[i]["amount"]
if fund<amount:
return False
else:
return True
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 = (11 - (int(abs(i.total_with()/total_spent)*10)+1)) * ' ' + \
(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
spend_chart = 'Percentage spent by category\n100|' + ' '+d[0][0]+' '+d[1][0]+' '+d[2][0]+' \n'\
' 90|' + ' '+d[0][1]+' '+d[1][1]+' '+d[2][1]+' \n'\
' 80|' + ' '+d[0][2]+' '+d[1][2]+' '+d[2][2]+' \n'\
' 70|' + ' '+d[0][3]+' '+d[1][3]+' '+d[2][3]+' \n'\
' 60|' + ' '+d[0][4]+' '+d[1][4]+' '+d[2][4]+' \n'\
' 50|' + ' '+d[0][5]+' '+d[1][5]+' '+d[2][5]+' \n'\
' 40|' + ' '+d[0][6]+' '+d[1][6]+' '+d[2][6]+' \n'\
' 30|' + ' '+d[0][7]+' '+d[1][7]+' '+d[2][7]+' \n'\
' 20|' + ' '+d[0][8]+' '+d[1][8]+' '+d[2][8]+' \n'\
' 10|' + ' '+d[0][9]+' '+d[1][9]+' '+d[2][9]+' \n'\
' 0|' + ' '+d[0][10]+' '+d[1][10]+' '+d[2][10]+' \n'\
' ----------\n'+temp_text
return(spend_chart)