Tell us what’s happening:
I’ve checked my create_spend_chart function many times, but still, I don’t know why my code fails the last test case. I compared my output with the last test case output and found that categories are only placed differently. What’s the problem with create_spend_chart function? All other functions seem to be alright and pass all the tests. Any help would be greatly appreciated.
Your code so far:
class Category:
instances = []
def __init__(self, name):
self.name = name
self.ledger = []
Category.instances.append(self)
def deposit(self, amount, description=""):
self.ledger.append({"amount": amount, "description": description})
def withdraw(self, amount, description=""):
if self.check_funds(amount) is False:
return False
self.ledger.append({"amount": -amount, "description": description})
return True
def get_balance(self):
return sum([item["amount"] for item in self.ledger])
def transfer(self, amount_to_transfer, where_to_transfer):
if self.check_funds(amount_to_transfer) is False:
return False
self.withdraw(amount_to_transfer, f"Transfer to {where_to_transfer.name}")
where_to_transfer.deposit(amount_to_transfer, f"Transfer from {self.name}")
return True
def check_funds(self, amount):
if self.get_balance() >= amount:
return True
else:
return False
def __str__(self):
title = ""
for x in range(15 - (len(self.name)//2)):
title += "*"
title += self.name
for x in range(30 - len(title)):
title += "*"
content = ""
for item in self.ledger:
content += item["description"][:23:]
for x in range(24 - len(item["description"][:23:])):
content += " "
content += "{:.2f}".format(item["amount"])
content += "\n"
content += "Total: {:.2f}".format(self.get_balance())
return title + "\n" + content
def create_spend_chart(instances):
output = "Percentage spent by category\n"
percentage = 100
sum_of_withdraws = 0
for instance in instances:
sum_of_withdraws += -sum([x["amount"] for x in instance.ledger if x["amount"]
< 0])
while percentage >= 0:
if len(str(percentage)) == 3:
output += str(percentage) + "|"
elif len(str(percentage)) == 2:
output += " " + str(percentage) + "|"
else:
output += " " + str(percentage) + "|"
for instance in instances:
spent = (-sum([x["amount"] for x in instance.ledger if x["amount"] <
0]))*100//(sum_of_withdraws)
if spent < 10:
spent = 0
else:
if int(str(spent)[1]) >= 5:
change_spent = str(spent).split(".")
value = int(change_spent[0])
while value % 10 != 0:
value += 1
spent = value
if spent >= percentage:
output += " o "
else:
output += " "
output += " \n"
percentage -= 10
output += " -"
for x in range(len(Category.instances)*3):
output += "-"
output += "\n"
longest_instance_name = 0
for instance in instances:
if len(instance.name) > longest_instance_name:
longest_instance_name = len(instance.name)
updated_names = []
for instance in instances:
while len(instance.name) < longest_instance_name:
instance.name += " "
updated_names.append(instance.name)
if len(updated_names) == 1:
for letter in updated_names[0]:
output += f" {letter} \n"
elif len(updated_names) == 2:
for letter in range(len(updated_names[0])):
output += f" {updated_names[0][letter]} {updated_names[1][letter]} \n"
elif len(updated_names) == 3:
for letter in range(len(updated_names[0])):
output += f" {updated_names[0][letter]} {updated_names[1][letter]}
{updated_names[2][letter]} \n"
elif len(updated_names) == 4:
for letter in range(len(updated_names[0])):
output += f" {updated_names[0][letter]} {updated_names[1][letter]}
{updated_names[2][letter]} {updated_names[3][letter]} \n"
return output.rstrip() + " "
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36
.
Challenge: Budget App
Link to the challenge: https://repl.it/@freeCodeCamp/fcc-budget-app
https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/budget-app