Tell us what’s happening:
Can anyone shed some light on what I’m missing? I’ve checked the spacing in a fixed-width font several times, separated the categories by two spaces. It’s the last two objectives about the category names, and how it should print a different chart. Greatly appreciated.
Your code so far
class Category:
def __init__(self, name):
self.ledger = []
self.name = name
def __str__(self):
breakdown = self.name.center(30, "*")
breakdown += "\n"
for element in self.ledger:
left = element["description"][:23].ljust(23, " ")
right = f"{element['amount']:.2f}".rjust(7, " ")
breakdown += left + right + "\n"
breakdown += f"Total: {self.get_balance()}"
return breakdown
def deposit(self, amount, description=""):
self.ledger.append({"amount":amount, "description":description})
def withdraw(self, amount, description=""):
if not self.check_funds(amount):
return False
self.ledger.append({"amount":-amount, "description":description})
return True
def get_balance(self):
balance = sum(t["amount"] for t in self.ledger)
return balance
def check_funds(self, amount):
balance = self.get_balance()
return (balance - amount >= 0)
def transfer(self, amount, receiver):
if not self.check_funds(amount):
return False
self.ledger.append({"amount":-amount, "description":f"Transfer to {receiver.name}"})
receiver.ledger.append({"amount":amount, "description":f"Transfer from {self.name}"})
return True
def calculate_percentages(categories):
all_spending = { }
for category in categories:
deductions = [t["amount"] for t in category.ledger if t["amount"] < 0]
spending = abs(sum(deductions))
all_spending[category.name] = spending
all_spending_sum = sum(all_spending.values()) or 1
percentages = {
cat: int((amnt / all_spending_sum * 100) // 10) * 10
for cat,amnt in all_spending.items()
}
return percentages
def create_spend_chart(categories):
percentages = calculate_percentages(categories)
lines = "Percentage spent by category\n"
for row in range(100, -1, -10):
empty_header = " " * 4
extra_pad = " " * 2
header = str("{0}| ".format(str(row).rjust(3, " ")))
tokens = []
for c in categories:
t = "o" if percentages[c.name] >= row else " "
tokens.append(t)
lines += header + (" ".join(tokens)+extra_pad) + "\n"
lines += empty_header + ("---" * len(categories)) + "-"
return lines
def create_category_names(categories):
lines = ""
names = [list(c.name) for c in categories]
max_len = max([len(c.name) for c in categories])
for i in range(0, max_len):
lines += " " * 5
for j, char_list in enumerate(names):
if i < len(char_list):
lines += char_list[i] + " "
else:
lines += " "
lines += "\n"
return lines
def main():
food = Category('Food')
food.deposit(1000, 'deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
clothing.withdraw(30)
clothing.withdraw(10)
auto = Category('Auto')
auto.deposit(200)
auto.withdraw(100)
output = create_spend_chart([food, clothing, auto]) + "\n"
output += create_category_names([food, clothing, auto])
print(output)
main()
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0
Challenge Information:
Build a Budget App - Build a Budget App