Hey, so i’m having trouble passing the last 2 tests. Everything looks good in the preview but I see in the console that the spacing is off. I’ve tried playing with my code, but i can’t seem to fix it. Here is the console error: AssertionError: ’ [140 chars] m \n e \n n \n t’ != ’ [140 chars] m \n e \n n \n t ’
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):
balance = sum(item['amount'] for item in self.ledger)
return balance
def transfer(self, amount, category):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {category.name}")
category.deposit(amount, f"Transfer from {self.name}")
return True
return False
def check_funds(self, amount):
return amount <= self.get_balance()
def __str__(self):
title = f"{self.name:*^30}\n"
items = ""
for entry in self.ledger:
description = entry['description'][:23]
amount = f"{entry['amount']:.2f}"
items += f"{description:<23}{amount:>7}\n"
total = f"Total: {self.get_balance():.2f}"
return title + items + total
def create_spend_chart(categories):
total_spent = 0
spent_per_category = []
for category in categories:
spent = sum(-entry['amount'] for entry in category.ledger if entry['amount'] < 0)
spent_per_category.append({'name': category.name, 'spent': spent})
total_spent += spent
for item in spent_per_category:
item['percentage'] = int((item['spent'] / total_spent) * 100)
chart = "Percentage spent by category\n"
for percent in range(100, -1, -10):
chart += f"{percent:>3}| "
for item in spent_per_category:
chart += "o " if item['percentage'] >= percent else" "
chart += "\n"
chart += " " + "-" * (len(categories) * 3 + 1) + "\n"
max_length = max(len(category.name) for category in categories)
for i in range(max_length):
line = " "
for category in categories:
line += category.name[i] + " " if i < len(category.name) else " "
chart += line + "\n"
return chart.rstrip()
food = Category("Food")
food.deposit(1000, "initial deposit")
food.withdraw(10.15, "groceries")
food.withdraw(15.89, "restaurant and more food for dessert")
clothing = Category("Clothing")
clothing.deposit(500, "initial deposit")
clothing.withdraw(25.55, "clothes shopping")
entertainment = Category("Entertainment")
entertainment.deposit(300, "initial deposit")
entertainment.withdraw(50, "movies and games")
food.transfer(50, clothing)
print(food)
print(clothing)
print(entertainment)
print(create_spend_chart([food, clothing, entertainment]))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project
You might not be able to actually highlight your output this way in any case. The point being that you need spaces to square off the output after the last character
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):
balance = sum(item['amount'] for item in self.ledger)
return balance
def transfer(self, amount, category):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {category.name}")
category.deposit(amount, f"Transfer from {self.name}")
return True
return False
def check_funds(self, amount):
return amount <= self.get_balance()
def __str__(self):
title = f"{self.name:*^30}\n"
items = ""
for entry in self.ledger:
description = entry['description'][:23]
amount = f"{entry['amount']:.2f}"
items += f"{description:<23}{amount:>7}\n"
total = f"Total: {self.get_balance():.2f}"
return title + items + total
def create_spend_chart(categories):
total_spent = 0
spent_per_category = []
for category in categories:
spent = sum(-entry['amount'] for entry in category.ledger if entry['amount'] < 0)
spent_per_category.append({'name': category.name, 'spent': spent})
total_spent += spent
for item in spent_per_category:
item['percentage'] = int((item['spent'] / total_spent) * 100 // 10) * 10
chart = "Percentage spent by category\n"
for percent in range(100, -1, -10):
chart += f"{percent:>3}| "
for item in spent_per_category:
chart += "o " if item['percentage'] >= percent else " "
chart += "\n"
chart += " " + "-" * (len(categories) * 3 + 1) + "\n"
max_length = max(len(category.name) for category in categories)
for i in range(max_length):
line = " "
for category in categories:
line += f"{category.name[i]} "
else:
line += " "
chart += line.rstrip() + "\n"
food = Category("Food")
food.deposit(1000, "initial deposit")
food.withdraw(10.15, "groceries")
food.withdraw(15.89, "restaurant and more food for dessert")
clothing = Category("Clothing")
clothing.deposit(500, "initial deposit")
clothing.withdraw(25.55, "clothes shopping")
entertainment = Category("Entertainment")
entertainment.deposit(300, "initial deposit")
entertainment.withdraw(50, "movies and games")
food.transfer(50, clothing)
print(food)
print(clothing)
print(entertainment)
print(create_spend_chart([food, clothing, entertainment]))
this is the part that i changed and now i’m getting ‘IndexError: string index out of range’
this is the part that i updated
max_length = max(len(category.name) for category in categories)
for i in range(max_length):
line = " "
for category in categories:
line += f"{category.name[i]} "
else:
line += " "
chart += line.rstrip() + "\n"
Traceback (most recent call last):
File "main.py", line 85, in <module>
File "main.py", line 64, in create_spend_chart
IndexError: string index out of range
Be sure and read more than just the first line of the failing message. The ability to read and comprehend error messages is a skill you’ll need to acquire as a developer. Ask questions on what you don’t understand.
The error message is pointing you to line 64. Look at what’s there and what could be the problem.