Tell us what’s happening:
I don’t understand why I am failing the last two test. Can someone take a look and tell me why I am failing these test?
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
def __str__(self):
result = self.name.center(30, '*') + '\n'
for item in self.ledger:
description = item['description'][:23]
amount = f'{item["amount"]:7.2f}'
result += f"{description:<23}{amount}\n"
result += f"Total: {self.get_balance():.2f}"
return result
def get_balance(self):
balance = 0
for item in self.ledger:
balance += item['amount']
return balance
def deposit(self, amount, description=''):
self.ledger.append({
'amount': amount,
'description': description})
def withdraw(self, amount, description=''):
avail_balance = self.get_balance()
if avail_balance >= amount:
self.ledger.append({
'amount': -amount,
'description': description
})
return True
return False
def check_funds(self, amount):
if self.get_balance() < amount:
return False
return True
def transfer(self, amount, category):
if not self.check_funds(amount):
return False
self.withdraw(amount, f'Transfer to {category.name}'
)
category.deposit(amount, f'Transfer from {self.name}')
return True
def create_spend_chart(categories):
spent = []
total = 0
for cat in categories:
cat_spent = sum(-item['amount'] for item in cat.ledger if item['amount'] < 0)
spent.append(cat_spent)
total += cat_spent
percentages = [(int((s / total) * 100) // 10) * 10 for s in spent]
chart = "Percentage spent by category\n"
for level in range(100, -1, -10):
chart += f"{level:>3}| "
for percent in percentages:
if percent >= level:
chart += "o "
else:
chart += " "
chart += "\n"
chart += " " + "-" * (len(categories) * 3 + 1) + "\n"
max_len = max(len(cat.name) for cat in categories)
for i in range(max_len):
chart += " "
for cat in categories:
if i < len(cat.name):
chart += cat.name[i] + " "
else:
chart += " "
chart += " \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/144.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App - Build a Budget App