Tell us what’s happening:
Hello, when finalizing my code I have two tests that fail and I can’t seem to understand why. Both errors, 23 and 24, tell me to fix my spacing as shown in the example output. I looked at my code and made sure all the requirements were met, double lines in between each category. I have asked AI to fix it but it still doesn’t work. Someone please help me I am very confused.
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 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 self.get_balance() >= amount
def __str__(self):
# Title line of 30 characters (Centering the category name)
title = f"{self.name:*^30}"
# Ledger lines (description and amount)
ledger_lines = ""
for item in self.ledger:
description = item['description'][:23] # Only first 23 characters of description
amount = f"{item['amount']:>7.2f}" # Amount with 2 decimal places, right aligned
ledger_lines += f"{description:<23}{amount}\n"
# Total line
total = self.get_balance()
total_line = f"Total: {total:0.2f}"
return title + "\n" + ledger_lines + total_line
def create_spend_chart(categories):
# Calculate total spent
total_spent = sum(
sum(item['amount'] for item in category.ledger if item['amount'] < 0) for category in categories
)
# Calculate the percentage spent for each category
percentages = []
for category in categories:
spent = sum(item['amount'] for item in category.ledger if item['amount'] < 0)
if total_spent == 0:
percentages.append(0)
else:
percentages.append(int(spent / total_spent * 100))
# Generate the chart
chart = "Percentage spent by category\n"
# Create the chart from 100 down to 0
for i in range(100, -1, -10):
chart += f"{i:3}| " # Add the percentage label
for percentage in percentages:
if percentage >= i:
chart += "o "
else:
chart += " "
chart += "\n"
# Add the horizontal line with the correct number of dashes
chart += " " + "-" * (len(categories) * 3 + 1) + "\n" # Horizontal line with three dashes per category and two extra spaces
# Add category names vertically
max_name_length = max(len(category.name) for category in categories)
# For each row of vertical category names
for i in range(max_name_length):
chart += " " # Adjusting space before category names
for category in categories:
if i < len(category.name):
chart += category.name[i] + " "
else:
chart += " " # This ensures the spacing is uniform
chart += "\n"
# Remove the extra spaces at the end of the final row
return chart.strip()
# Example usage:
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(50, 'new shoes')
auto = Category('Auto')
auto.deposit(2000, 'initial deposit')
auto.withdraw(150, 'gas')
# Transfers between categories
food.transfer(50, clothing)
# Print individual categories
print(food)
print(clothing)
print(auto)
# Print the spend chart
categories = [food, clothing, auto]
print(create_spend_chart(categories))
Your browser information:
User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project