So, here is my code:
class Category:
def init(self,name):
self.name = name
self.ledger =
def deposit(self,amount, description=""):
self.ledger.append({"amount":amount, "description":description})
return self.ledger
def check_funds(self,amount):
total = 0
for i in range(len(self.ledger)):
total += self.ledger[i]['amount']
if total >= amount:
return True
else:
return False
def withdraw(self,amount, description=""):
if Category.check_funds(self,amount) == True:
self.ledger.append({"amount":-amount, "description":description})
return True
else:
return False
def get_balance(self):
total = 0
for i in range(len(self.ledger)):
total += self.ledger[i]['amount']
return total #unformated
#return "{:.2f}".format(total)
def transfer(self,amount,destination):
self.destination = destination
#make new withdraw instructions
if Category.check_funds(self,amount) == True:
#if return is True, append to ledger
self.ledger.append({"amount":-amount, "description":"Transfer to "+destination.name})
#make new deposit instructions
destination.ledger.append({"amount":amount, "description":"Transfer from "+self.name})
return True
else:
return False
def __str__(self):#amount, description?
Formatted = "{:*^30}"
title= (Formatted.format(self.name)) +'\n'
#get_balance and store in total_balance variable
total_balance = Category.get_balance(self)
#total_balance = "{:.2f}".format(total_balance)
#convert to decimal with two digits after period, if not already a decimal
items = ''
self.items_lst = []
#print(self.ledger[0].values())
for i in range(len(self.ledger)):
if len(str("{:.2f}".format(self.ledger[i]['amount']))) > 7:
items += self.ledger[i]['description'][:22]+str("{:.2f}".format(self.ledger[i]['amount'])[:8]).rjust(30-(len(self.ledger[i]['description'][:23])))+'\n'
else:
items += self.ledger[i]['description'][:23]+str("{:.2f}".format(self.ledger[i]['amount'])[:7]).rjust(30-(len(self.ledger[i]['description'][:23])))+'\n'
return title + items+"Total: " + str(total_balance)
def create_spend_chart(categories):
category_dict = dict()
for items in categories:
total = 0
for pairs in items.ledger:
for index in pairs:
if index == ‘amount’:
if pairs[index] < 0:
pairs[index] = -pairs[index]
total += pairs[index]
category_dict[items.name] = total
spending_total = 0
for index in category_dict:
spending_total += category_dict[index]
percents_dict = dict()
for index in category_dict:
percent = (category_dict[index] / spending_total) * 100
percent = “{:.0f}”.format(percent)
percent = int(percent)
#percent = round(percent/10)*10
percent = (percent//10)*10
percents_dict[index] = percent
#print(percents_dict)
percents_lst = list()
for i in percents_dict:
percents_lst.append(percents_dict[i])
final_percent_lst = list()
percents = ['100|', '90|', '80|','70|','60|','50|','40|','30|','20|','10|','0|']
final_percent_lst.append(percents)
for percent in percents_lst:
percent_lst = [' '] * 11
for i in range(len(percent_lst[0:percent//10 + 1])):
percent_lst[i] = 'o'
percent_lst = percent_lst[::-1] #reverse each list
final_percent_lst.append(percent_lst)
vertical_axis = list(zip(*final_percent_lst))
max_category_length = list()
category_name_lst = list()
for category in percents_dict:
max_category_length.append(len(category))
for cat_name in percents_dict:
category_name_lst.append(cat_name)
full_category_name_letter_lst = list()
for category in category_name_lst:
full_category_name_letter_lst.append(list(category))
final_category_letter_lst = list()
for name in full_category_name_letter_lst:
category_letters = [' '] * max(max_category_length)
category_letters_lst = name + category_letters[:max(max_category_length) - len(name)]
final_category_letter_lst.append(category_letters_lst)
horizontal_axis = list(zip(*final_category_letter_lst))
vertical = ''
horizontal = ''
bottom_line = ''
title = str('Percentage spent by category') + '\n'
if len(percents_lst) == 1:
for y in vertical_axis:
vertical += str(y[0]).rjust(4)+str(y[1]).rjust(2)+' '+'\n'
if y[0] == '0|':
bottom_line = str('-' * (len(y)+2)).rjust(8)+ '\n'
for x in horizontal_axis:
horizontal += str(x[0]).rjust(6) +' ' + '\n'
return title + vertical + bottom_line + horizontal
if len(percents_lst) == 2:
for y in vertical_axis:
vertical += str(y[0]).rjust(4)+str(y[1]).rjust(2)+str(y[2]).rjust(2)+' '+'\n'
if y[0] == '0|':
bottom_line = str('-' * (len(y)+3)).rjust(10)+ '\n'
for x in horizontal_axis:
horizontal += str(x[0]).rjust(6)+str(x[1]).rjust(2) +' ' +'\n'
return title + vertical + bottom_line + horizontal
if len(percents_lst) == 3:
for y in vertical_axis:
vertical += str(y[0]).rjust(4)+str(y[1]).rjust(2)+str(y[2]).rjust(3)+str(y[3]).rjust(3)+' '+'\n'
if y[0] == '0|':
bottom_line = str('-' * (len(y)+6)).rjust(14)+ '\n'
for x in horizontal_axis:
horizontal += str(x[0]).rjust(6)+str(x[1]).rjust(3)+str(x[2]).rjust(3) +' ' +'\n'
return title + vertical + bottom_line + horizontal
if len(percents_lst) >= 4:
for y in vertical_axis:
vertical += str(y[0]).rjust(4)+str(y[1]).rjust(2)+str(y[2]).rjust(2)+str(y[3]).rjust(2)+str(y[4]).rjust(2) +' '+'\n'
if y[0] == '0|':
bottom_line = str('-' * (len(y)+4)).rjust(14)+ '\n'
for x in horizontal_axis:
horizontal += str(x[0]).rjust(6)+str(x[1]).rjust(2)+str(x[2]).rjust(2)+str(x[3]).rjust(2)+' ' +'\n'
return title + vertical + bottom_line + horizontal
I have enclosed a screen shot of the Repl.it error:
I don’t know what the error is telling me. Please help!
