Tell us what’s happening:
Hi there. The only remaining test that is failing for me is number 16 - “Printing a category instance should give a different string representation of the object”. I have tried using the same exact data as is in the example, and my output looks identical to me, but the test is still failing. Is there anything obvious about my code that’s wrong, I’m very stuck.
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):
withdrawal = 0-amount
self.ledger.append({'amount': withdrawal, 'description': description})
return True
return False
def get_balance(self):
balance = sum([t.get("amount") for t in self.ledger])
return balance
def transfer(self, amount, receiver):
if not self.check_funds(amount): return False
else:
self.withdraw(amount, "Transfer to " + receiver.name)
receiver.ledger.append({'amount': amount, 'description': "Transfer from " + self.name})
return True
def check_funds(self, amount):
balance = sum([t.get("amount") for t in self.ledger])
return False if amount > balance else True
def __str__(self):
length = len(self.name)
stars = int((30 - length) / 2)
title_line = ("*" * stars) + self.name + ("*" * (30-length-stars))
entries = ""
for index, l in enumerate(self.ledger):
if index == 0:
i = "initial deposit"
id = self.ledger[0]["amount"]
entries += f"{i:<23}{id:>7.2f}\n"
else:
e = self.ledger[index]["description"]
a = self.ledger[index]["amount"]
entries += f"{e[0:22]:<23}{a:>7.2f}\n"
total = float(sum([t.get("amount") for t in self.ledger]))
return f"{title_line}\n{entries}Total: {total:.2f}"
#-------------------------------------------------
#CHART DRAWING
def create_spend_chart(categories):
spentlist = []
per = []
# Find the total amount spent (excludes transfers to other accounts)
for c in categories:
spent = 0
for t in c.ledger:
if t["amount"] < 0 and "Transfer to" not in t["description"]: spent-= t["amount"]
else: spent -= 0
spentlist.append(abs(spent))
totalspend = sum(spentlist)
# Find the percentage spent per category
for l in spentlist:
cat_spent = (l/totalspend) * 100
if round(cat_spent, -1) > cat_spent:
per.append(round(cat_spent, -1) - 10)
else:
per.append(round(cat_spent, -1))
# Figure out how to make the chart work now we have the data
chart = "Percentage spent by category\n"
for n in range (100, -10, -10):
numbers = f"{n:>3}|"
for p in per:
if p >= n: onum = " o "
else: onum = " "
numbers += onum
numbers += " "
chart += f"{numbers}\n"
dashes = "-" * len(categories)*3
chart += " " + dashes + "-"
names = [c.name for c in categories]
maxlen = len(max(names, key = len))
# Category names on the chart
for i in range(0, maxlen):
barname = " "
for n in names:
if i < len(n): barname += f" {n[i]} "
else: barname += " "
barname += " "
chart += f"\n{barname}"
return chart
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:147.0) Gecko/20100101 Firefox/147.0
Challenge Information:
Build a Budget App - Build a Budget App

