I don’t know what the issue is?
Test 16-24 are failed due to create_spend_chart() function.
class Category:
def __init__(self,name):
self.name = name
self.ledger = []
def deposit(self,amount:int,description=""):
self.ledger.append({"amount":amount,'description':description})
def check_funds(self,amount:int):
if amount <= self.get_balance():
return True
else:
return False
def get_balance(self):
return sum([x.get("amount") for x in self.ledger])
def withdraw(self,amount:int,description=""):
if self.check_funds(amount):
self.ledger.append({"amount":(amount*-1),'description':description})
return True
else:
return False
def transfer(self,amount:int,destination):
if self.check_funds(amount):
des = f"Transfer to {destination.name}"
self.withdraw(amount,des)
des = f"Transfer from {self.name}"
destination.deposit(amount,des)
return True
else:
return False
def __str__(self):
d = self.name.capitalize()
d = d.center(30,"*")
for x in self.ledger:
d+= f"\n{x.get('description')}\t{float(x.get('amount'))}"
d+= f"\n Total: {self.get_balance()}"
return d
def create_spend_chart(categories):
print("Percentage spent by category.")
length = len(categories)
ch = "o"
spent = []
percentage_list = []
name_list = []
for categorie in categories:
spent.append(int(categorie.ledger[0]["amount"]-categorie.get_balance()))
for item in spent:
percentage = (item/sum(spent))*100
percentage_list.append(round(percentage/10)*10)
del spent
for x in sorted(percentage_list,reverse=True):
index = percentage_list.index(x)
name_list.append(categories[index].name)
for i in range(100,-10,-10):
s = str(i)
data = f"{' '*(3-len(s))+s}|"
data_1 = ""
for item in percentage_list:
if item >= i:
data_1 += ' o '
print(data+data_1)
print(' '*4+"-"*(len(percentage_list)*3))
max_len = max([len(i) for i in name_list])
space = 1 if length % 2 == 0 else 0
for i in range(max_len):
data = ' '*5
for k,x in enumerate(name_list):
if len(x)-1 >= i:
if space:
data+= f"{x[i]}"+(" "*(length))
else:
data+= f"{x[i]}"+(" "*(length-(space+1)))
else:
data+= " "*(length+space)
continue
print(data)
# create_spend_chart("d")
# food = Category('Food')
# food.deposit(1000, 'deposit')
# food.withdraw(150, 'groceries')
# food.withdraw(15.56, 'restaurant and more food for dessert')
# clothing = Category('Clothing')
# food.transfer(50, clothing)
# clothing.withdraw(45,"test")
# aa = Category("IDK")
# food.transfer(250, aa)
# aa.withdraw(150,"D")
# create_spend_chart([food,clothing,aa])
https://www.freecodecamp.org/learn/python-v9/lab-budget-app/build-a-budget-app