Tell us what’s happening:
Your code so far
def create_spend_chart(categories):
title = "Percentage spent by category\n"
spent = []
for category in categories:
total = sum(-item["amount"] for item in category.ledger if item["amount"] < 0)
spent.append(total)
total_spent = sum(spent)
percentages = [(int((s / total_spent) * 100) // 10) * 10 for s in spent]
chart = title
for i in range(100, -1, -10):
line = f"{i:>3}| "
for p in percentages:
line += "o " if p >= i els
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 = 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.name}")
category.deposit(amount, f"Transfer from {self.name}")
return True
return False
def check_funds(self, amount):
if amount > self.get_balance():
return False
return True
def __str__(self):
title = self.name.center(30, "*") + "\n"
items = ""
for entry in self.ledger:
desc = entry["description"][:23]
amount = "{:.2f}".format(entry["amount"])
items += f"{desc:<23}{amount:>7}\n"
total = f"Total: {self.get_balance():.2f}"
return title + items + total
food=Category('Food')
food.deposit(900,'deposit')
food.withdraw(-45.67,'milk, cereal,eggs, bacon, bread')
food.transfer(900,food)
def create_spend_chart(categories):
title = "Percentage spent by category\n"
spent = []
for category in categories:
total = sum(-item["amount"] for item in category.ledger if item["amount"] < 0)
spent.append(total)
total_spent = sum(spent)
percentages = [(int((s / total_spent) * 100) // 10) * 10 for s in spent]
chart = title
for i in range(100, -1, -10):
line = f"{i:>3}| "
for p in percentages:
line += "o " if p >= i else " "
chart += line + "\n"
chart += " " + "-" * (len(categories) * 3 + 1) + "\n"
names = [c.name for c in categories]
max_len = max(len(name1) for name1 in names)
for i in range(max_len):
line = " "
for name1 in names:
if i < len(name1):
line += name1[i] + " "
else:
line += " "
chart += line + "\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/147.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App - Build a Budget App
