Tell us what’s happening:
I’ve been at this for the past couple of hours but I can’t seem to get why tests 22 and 23 aren’t passing, I’ve checked and tweaked my code over and over again but nothing seems to working. someone please help. Here’s what my console is showing me:
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
def get_balance(self):
amounts = [i['amount'] for i in self.ledger]
return sum(amounts)
def check_funds(self, amount):
balance = self.get_balance()
if amount > balance:
return False
return True
def deposit(self, amount, desc = ""):
self.ledger.append({
'amount': amount,
'description': desc
})
# return self.get_balance()
def withdraw(self, amount, desc = ""):
if self.check_funds(amount):
self.ledger.append({
'amount': -amount,
'description': desc
})
return True
else:
return False
def transfer(self, amount, to_cat):
if self.check_funds(amount):
self.withdraw(amount, desc = f"Transfer to {to_cat.name}")
to_cat.deposit(amount, desc = f"Transfer from {self.name}")
return True
else:
return False
def __str__(self):
# name_length = len(self.name)
# num_stars = 30 - name_length
details = ''
for entry in self.ledger:
amount = "{:.2f}".format(round(float(entry['amount']), 2))
desc = entry['description'][:23]
details += f'{desc.ljust(23)}{str(amount).rjust(7)}'
details += '\n'
details += 'Total: {:.2f}'.format(self.get_balance())
return (
self.name.center(30, '*') + '\n' + details
)
def create_spend_chart(categories):
chart = 'Percentage spent by category\n'
# chart = ''
withdrawals = {}
total_spent = 0
for cat in categories:
entries = []
for entry in cat.ledger:
if entry['amount'] < 0:
entries.append(abs(entry['amount']))
withdrawals[cat.name] = entries
for w in withdrawals.values():
total_spent += sum(w)
percentages = {k: int(sum(v)) for k, v in withdrawals.items()}
print(percentages)
n_cats = len(percentages.keys())
# for v in percentages.values():
# rows = [(str(i) + '|' + 'o' if v == i else str(i) + '|' + ' ') for i in range(100, -10, -10)]
for i in range(100, -10, -10):
rows = f'{i:3}| '
for name in percentages:
perc = percentages[name]
if perc >= i:
rows += 'o '
else:
rows += ' '
chart += rows + '\n'
chart += " " + "-" * (n_cats * 3 + 1) + "\n"
max_k = max(len(k) for k in percentages)
for i in range(max_k):
row = " " * 5
for k in percentages:
if i < len(k):
row += k[i] + " "
else:
row += " "
chart += row + '\n'
# print(chart_title)
print(chart.strip())
return chart.strip()
food = Category('Food')
food.deposit(1000, 'initial 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(18, 'adfsasa')
print(food)
# print(food.get_balance())
# print(food.ledger)
print(clothing)
create_spend_chart([food, clothing])
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App - Build a Budget App
