Tell us what’s happening:
Hi, i finish my code but i can’t see whats wrong with it…
Can i get some help?
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.money = 0
def __str__(self):
title = self.name.center(30, "*") + "\n"
items = ""
for item in self.ledger:
items += f"{item['description'][:23]:23}{item['amount']:>7.2f}\n"
items += f"Total: {self.money:.2f}"
return title + items
def deposit(self, amount, description = ""):
self.ledger.append({"amount": amount, "description": description})
self.money += amount
def withdraw(self, amount, description = ""):
if self.check_funds(amount):
self.deposit(-amount, description)
return True
else:
return False
def get_balance(self):
return self.money
def transfer(self, amount, another_category):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {another_category.name}")
another_category.deposit(amount, f"Transfer from {self.name}")
return True
return False
def check_funds(self, amount):
return self.get_balance() >= amount
def create_spend_chart(categories):
spending = {}
total_spent = 0
for category in categories:
spent = sum(item['amount'] for item in category.ledger if item['amount'] < 0)
spending[category.name] = -spent
total_spent += -spent
chart = "Percentage spent by category\n"
percentages = {cat: (spent / total_spent) * 100 for cat, spent in spending.items()}
highest_percentage = (max(percentages.values()) // 10 + 1) * 10
for i in range(100, -1, -10):
row = f"{i:>3}| "
for category in categories:
if percentages.get(category.name, 0) >= i:
row += "o "
else:
row += " "
chart += row.rstrip() + " \n"
chart += " -" + "---" * len(categories) + "\n"
max_length = max(len(cat.name) for cat in categories)
for i in range(max_length):
row = " "
for category in categories:
if i < len(category.name):
row += " " + category.name[i] + " "
else:
row += " "
chart += row.rstrip() + " \n"
return chart.rstrip()
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 OPR/114.0.0.0
Challenge Information:
Build a Budget App Project - Build a Budget App Project