Tell us what’s happening:
I need help with number 17 it says creat_spend_chart should print a different chart representation check all spacing is exact. I’ve tried fixing but nothing is happening. here’s my code.
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):
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):
total = sum(item['amount'] for item in self.ledger)
return total
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}"
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 + "\n" + items + total
def create_spend_chart(categories):
total_spent = 0
category_spent = {}
for category in categories:
spent = sum(-item['amount'] for item in category.ledger if item['amount'] < 0)
category_spent[category.name] = spent
total_spent += spent
# Calculate percentages
percentages = {name: int((spent / total_spent) * 100) // 10 * 10 for name, spent in category_spent.items()}
# Create chart
chart = "Percentage spent by category\n"
for i in range(100, -1, -10):
chart += f"{i:>3}| "
for name in percentages:
chart += "o " if percentages[name] >= i else " "
chart += "\n"
chart += " ----------\n"
# Prepare category names
max_length = max(len(name) for name in percentages)
for i in range(max_length):
chart += " "
for name in percentages:
chart += name[i] + " " if i < len(name) else " "
chart += "\n"
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')
food.transfer(50, clothing)
print(food)
print(clothing)
# Create spend chart example
print(create_spend_chart([food, clothing]))
Your browser information:
User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project