Tell us what’s happening:
My code works, but i still keep on getting error.
It says to check if all spacing is exact and to open browser console for more details.
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
def deposit(self, amount, description=""):
"""Adds a deposit to the ledger."""
self.ledger.append({'amount': amount, 'description': description})
def withdraw(self, amount, description=""):
"""Withdraws an amount from the ledger if sufficient funds exist."""
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': description})
return True
return False
def get_balance(self):
"""Returns the current balance of the category."""
balance = sum(item['amount'] for item in self.ledger)
return balance
def transfer(self, amount, category):
"""Transfers an amount to another category if sufficient funds exist."""
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):
"""Checks if sufficient funds exist for a withdrawal or transfer."""
return amount <= self.get_balance()
def __str__(self):
"""Returns a string representation of the category's ledger."""
title = f"{self.name:*^30}\n"
items = ""
for item in self.ledger:
description = item['description'][:23]
amount = f"{item['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):
"""Creates a spend chart from a list of categories."""
total_spent = 0
category_spent = {}
for category in categories:
spent = sum(-item['amount'] for item in category.ledger if item['amount'] < 0)
total_spent += spent
category_spent[category.name] = spent
percentage_spent = {name: (spent / total_spent) * 100 for name, spent in category_spent.items()}
chart = "Percentage spent by category\n"
for i in range(100, -1, -10):
chart += f"{i:>3}| "
for name in percentage_spent:
if percentage_spent[name] >= i:
chart += "o "
else:
chart += " "
chart += "\n"
chart += " " + "-" + "---" * len(percentage_spent) + "\n"
max_length = max(len(name) for name in percentage_spent)
for i in range(max_length):
chart += " "
for name in percentage_spent:
if i < len(name):
chart += name[i] + " "
else:
chart += " "
chart += "\n"
return chart.rstrip()
if __name__ == "__main__":
food = Category("Food")
food.deposit(1000, "Initial deposit")
food.withdraw(200, "Groceries")
food.withdraw(150, "Dining out")
clothing = Category("Clothing")
clothing.deposit(500, "Initial deposit")
clothing.withdraw(50, "T-shirt")
entertainment = Category("Entertainment")
entertainment.deposit(300, "Initial deposit")
entertainment.withdraw(90, "Movies")
print(food)
print(clothing)
print(entertainment)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project