Everything I wrote is correct and even displays correctly yet is still says that the create_spend_chart() function isnt displaying the chart correctly. I dont know why please help me
Your code so far
class Category:
def __init__(self, name):
self.name = name
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, category):
if not self.check_funds(amount):
return False
self.withdraw(amount, f"Transfer to {category.name}")
category.deposit(amount, f"Transfer from {self.name}")
return True
def check_funds(self, amount):
return amount <= self.get_balance()
def __str__(self):
title = f"{self.name:*^30}\n"
items = "".join(f"{item['description'][:23]:23}{item['amount']:>7.2f}\n" for item in self.ledger)
total = f"Total: {self.get_balance():.2f}"
return title + items + total
def create_spend_chart(categories):
spent = [sum(-item["amount"] for item in cat.ledger if item["amount"] < 0) for cat in categories]
total_spent = sum(spent)
percentages = [int((amount / total_spent) * 10) * 10 for amount in spent]
chart = "Percentage spent by category\n"
for i in range(100, -1, -10):
chart += f"{i:>3}| " + " ".join("o" if percent >= i else " " for percent in percentages) + " \n"
chart += " " + "-" * (3 * len(categories) + 2) + "\n"
max_len = max(len(cat.name) for cat in categories)
names = [cat.name.ljust(max_len) for cat in categories]
for i in range(max_len):
chart += " " + " ".join(name[i] for name in names) + " \n"
return chart.rstrip()
food = Category("Food")
clothing = Category("Clothing")
entertainment = Category("Entertainment")
food.deposit(1000, "initial deposit")
food.withdraw(10.15, "groceries")
food.withdraw(15.89, "restaurant")
clothing.deposit(500, "initial deposit")
clothing.withdraw(50, "jeans")
entertainment.deposit(300, "initial deposit")
entertainment.withdraw(100, "movies")
print(food)
print(create_spend_chart([food, clothing, entertainment]))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:136.0) Gecko/20100101 Firefox/136.0
Challenge Information:
Build a Budget App Project - Build a Budget App Project
if you take that AssertionError line and put the two compared strings one below the other you should see the difference, yours is the first
for the diff that is below, I have added a description below
The assertion error and diff gives you a lot of information to track down a problem. For example:
AssertionError: 'Year' != 'Years'
- Year
+ Years
? +
Your output comes first, and the output that the test expects is second.
AssertionError: ‘Year’ != ‘Years’
Your output: Year does not equal what’s expected: Years
This is called a diff, and it shows you the differences between two files or blocks of code:
- Year
+ Years
? +
- Dash indicates the incorrect output + Plus shows what it should be ? The Question mark line indicates the place of the character that’s different between the two lines. Here a + is placed under the missing s .
Here’s another example:
E AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E assert ' 3801 123 \n - 2 + 49 \n------ ----- \n' == ' 3801 123\n- 2 + 49\n------ -----'
E - 3801 123
E + 3801 123
E ? ++++
E - - 2 + 49
E + - 2 + 49
E - ------ -----
E + ------ -----
E ? +++++
The first line is long, and it helps to view it as 2 lines in fixed width characters, so you can compare it character by character:
Again, your output is first and the expected output is second. Here it’s easy to see extra spaces or \n characters.
E - 3801 123
E + 3801 123
E ? ++++
Here the ? line indicates 4 extra spaces at the end of a line using four + symbols. Spaces are a little difficult to see this way, so it’s useful to use both formats together.
Ohhhhh it strips the spaces OKkkkk I Am reallyy sorryy I didnt even notice that I am really sorry for the inconvenience I wrote it out of habit and didnt even realize that it was returning the stripped version I am really sorry and Thanks for your help