This is my code but there is an error. create_spend_chart should print a different chart representation. Check that all spacing is exact.
How to fix it?
class Category:
def __init__(self, category):
self.category = category
self.ledger = []
def deposit(self, amount, description=""):
self.ledger.append({"amount": amount, "description": description})
def withdraw(self, amount, description=""):
if self.check_funds(amount):
self.ledger.append({"amount": -amount, "description": description})
return True
return False
def get_balance(self):
return sum(item["amount"] for item in self.ledger)
def transfer(self, amount, budget_category):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {budget_category.category}")
budget_category.deposit(amount, f"Transfer from {self.category}")
return True
return False
def check_funds(self, amount):
return self.get_balance() >= amount
def __str__(self):
title = f"{self.category:*^30}\n"
items = ""
for item in self.ledger:
items += f"{item['description'][:23]:23}{item['amount']:>7.2f}\n"
total = f"Total: {self.get_balance():.2f}"
return title + items + total
def create_spend_chart(categories):
chart = "Percentage spent by category\n"
# Calculate the percentage spent for each category
spendings = [sum(item["amount"] for item in category.ledger if item["amount"] < 0) for category in categories]
total_spent = sum(spendings)
percentages = [int(spending / total_spent * 100) for spending in spendings]
# Build the chart row by row
for i in range(100, -1, -10):
chart += str(i).rjust(3) + "| "
for percentage in percentages:
chart += "o" if percentage >= i else " "
chart += " "
chart += "\n"
# Add the bottom line
chart += " -" + "---" * len(categories) + "\n"
# Find the maximum category name length
max_name_length = max(len(category.category) for category in categories)
# Build the category names row by row
for i in range(max_name_length):
chart += " "
for category in categories:
if i < len(category.category):
chart += category.category[i] + " "
else:
chart += " "
chart += "\n"
return chart.rstrip()
# Example usage
food = Category("Food")
food.deposit(1000, "deposit")
food.withdraw(10.15, "groceries")
food.withdraw(15.89, "restaurant and more food for dessert")
clothing = Category("Clothing")
food.transfer(250, clothing)
clothing.deposit(1000, "deposit")
clothing.transfer(100, food)
print(create_spend_chart([food, clothing]))
Here is the expected output that you need to match:
Percentage spent by category\n100| \n 90| \n 80| \n 70| o \n 60| o \n 50| o \n 40| o \n 30| o \n 20| o o \n 10| o o \n 0| o o o \n ----------\n B F E \n u o n \n s o t \n i d e \n n r \n e t \n s a \n s i \n n \n m \n e \n n \n t
Percentage spent by category\n100| \n 90| \n 80| \n 70| o \n 60| o \n 50| o \n 40| o \n 30| o \n 20| o o \n 10| o o \n 0| o o o \n ----------\n B F E \n u o n \n s o t \n i d e \n n r \n e t \n s a \n s i \n n \n m \n e \n n \n t
Iāve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the āpreformatted textā tool in the editor (</>) to add backticks around text.
Not sure what happened but it looks like there now needs to be two extra spaces at the end. Here is the expected output (I added single quotes so you can see the two spaces at the end):
'Percentage spent by category\n100| \n 90| \n 80| \n 70| o \n 60| o \n 50| o \n 40| o \n 30| o \n 20| o o \n 10| o o \n 0| o o o \n ----------\n B F E \n u o n \n s o t \n i d e \n n r \n e t \n s a \n s i \n n \n m \n e \n n \n t '
Maybe it changed or maybe I didnāt copy it correctly in the first placeā¦
āPercentage spent by category\n100| \n 90| \n 80| \n 70| o \n 60| o \n 50| o \n 40| o \n 30| o \n 20| o o \n 10| o o \n 0| o o o \n ----------\n B F E \n u o n \n s o t \n i d e \n n r \n e t \n s a \n s i \n n \n m \n e \n n \n tā
I replace chart += "\n" to chart += " \n".
And also I used ā*ā instead space, after then I can see correct result.
But I canāt still pass this test.