Tell us what’s happening:
Passed all test, but the site does not want to tick complete and let me move on.
Your code so far
class Category:
def __init__(self, category):
self.category = category
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 = 0
for item in self.ledger:
total += item["amount"]
return total
def transfer(self, amount, category):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {category.category}")
category.deposit(amount, f"Transfer from {self.category}")
return True
return False
def check_funds(self, amount):
return amount <= self.get_balance()
def create_spend_chart(categories):
# Step 1: Calculate total spent
total_spent = 0
spent_per_category = []
for cat in categories:
spent = 0
for item in cat.ledger:
if item["amount"] < 0:
spent += -item["amount"]
spent_per_category.append(spent)
total_spent += spent
# Step 2: Convert to percentages
percentages = []
for spent in spent_per_category:
percent = int((spent / total_spent) * 100)
percent = percent - (percent % 10) # round down to nearest 10
percentages.append(percent)
# Step 3: Build the chart
chart = "Percentage spent by category\n"
for level in range(100, -1, -10):
chart += str(level).rjust(3) + "| "
for percent in percentages:
chart += "o " if percent >= level else " "
chart += "\n"
# Step 4: Add horizontal line
chart += " -" + "---" * len(categories) + "\n"
# Step 5: Add category names vertically
max_length = max(len(cat.category) for cat in categories)
for i in range(max_length):
chart += " "
for cat in categories:
if i < len(cat.category):
chart += cat.category[i] + " "
else:
chart += " "
chart += "\n"
return chart.rstrip("\n")
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project