Tell us what’s happening:
When running tests the steps 6,9,10 fail but when i test them manualy, the outpout is what it’s supose to be.
Your code so far
class Category:
def __init__(self, description):
self.description = description
self.ledger = []
self.__balance = 0.0
def __repr__(self):
header = self.description.center(30, "*") + "\n"
ledger = ""
for item in self.ledger:
# format description and amount
line_description = "{:<23}".format(item["description"])
line_amount = "{:>7.2f}".format(item["amount"])
# Truncate ledger description and amount to 23 and 7 characters respectively
ledger += "{}{}\n".format(line_description[:23], line_amount[:7])
total = "Total: {:.2f}".format(self.__balance)
return header + ledger + total
#append object to the ledger list
def deposit(self, amount, description=''):
self.ledger.append({'amount': amount, 'description': description})
self.__balance += amount
#pases the amount in the ledger as a negative number.
def withdraw(self, amount, description=''):
if self.__balance - amount >= 0:
self.ledger.append({'amount': -1 * amount, 'description': description})
self.__balance -= amount
return True
else:
return False
#return the current balance of the budget.
def get_blance(self):
return self.__balance
#transfer founds between categories
def transfer(self, amount, category_instance):
if self.withdraw(amount, "Transfer to {}".format(category_instance.description)):
category_instance.deposit(amount, "Transfer from {}".format(self.description))
return True
else:
return False
#checks if the amount exists in the balance
def check_funds(self, amount):
if self.__balance >= amount:
return True
else:
return False
def create_spend_chart(categories):
spent_amounts = []
#get total spent in each category
for category in categories:
spent = 0
for item in category.ledger:
if item["amount"] < 0:
spent += abs(item["amount"])
spent_amounts.append(round(spent, 2))
#calculate percentage rounded down
total = round(sum(spent_amounts), 2)
spent_percentage = list(map(lambda amount: int((((amount / total) * 10) // 1) * 10), spent_amounts))
#create the bar chart strings
header = "Percentage spent by category\n"
chart = ""
for value in reversed(range(0, 101, 10)):
chart += str(value).rjust(3) + '|'
for percent in spent_percentage:
if percent >= value:
chart += " o "
else:
chart += " "
chart += " \n"
footer = " " + "-" * ((3 * len(categories)) + 1) + "\n"
descriptions = list(map(lambda category: category.description, categories))
max_length = max(map(lambda description: len(description), descriptions))
descriptions = list(map(lambda description: description.ljust(max_length), descriptions))
for x in zip(*descriptions):
footer += " " + "".join(map(lambda s: s.center(3), x)) + " \n"
return (header + chart + footer).rstrip("\n")
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0
Challenge Information:
Build a Budget App Project - Build a Budget App Project