Tell us what’s happening:
Failing test 23, console output looks like my output matches the expected value. I tested the length lines printed below the chart, they’re all the same. It says 1 test OK, but then has error messages that I don’t understand. Any guidance is appreciated.
[Warning] n (python-test-evaluator.js, line 2)
[Warning] - t+ t ? ++ (python-test-evaluator.js, line 2)
[Warning] : Expected different category names written vertically below the bar. Check that all spacing is exact. (python-test-evaluator.js, line 2)
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):
self.ledger.append({'amount': -amount, 'description': description})
return True
return False
def check_funds(self, amount):
if amount > self.get_balance():
return False
return True
def get_balance(self):
balance = 0
for tx in self.ledger:
balance += tx['amount']
return balance
def transfer(self, amount, other):
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': f'Transfer to {other.name}'})
other.ledger.append({'amount': amount, 'description': f'Transfer from {self.name}'})
return True
return False
def __str__(self):
print_string = ''
available_width = 30 - len(self.name)
padding_left = available_width//2
padding_right = 30 - len(self.name) - padding_left
print_string += f"{('*' * padding_left)}{self.name}{('*' * padding_right)}\n"
for tx in self.ledger:
description = tx['description'][:23]
amount = f"{tx['amount']:.2f}".rjust(7)
print_string += f"{description.ljust(23)}{amount}\n"
print_string +=f"Total: {self.get_balance()}"
return print_string
def create_spend_chart(categories):
# print('Percentage spent by category')
chart = 'Percentage spent by category\n'
y_labels = [n for n in range(100, -1, -10)]
withdrawals = []
denominator = 0
for category in categories:
wd = 0
for tx in category.ledger:
if (tx['amount'] < 0):
wd -= tx['amount']
withdrawals.append((category.name, wd))
denominator += wd
percentages = []
for item in withdrawals:
percentage = (item[1]/denominator)*100
rounded_down = percentage//10 * 10
percentages.append(rounded_down)
for y in y_labels:
line = f"{y}| ".rjust(5)
for percent in percentages:
if percent >= y:
line +="o "
else:
line +=" "
# print(line)
chart += line + '\n'
# print(f" --{'---' * (len(categories) - 1)}--")
chart += f" --{'---' * (len(categories) - 1)}--\n"
max_length = max(len(cat.name) for cat in categories)
for i in range(max_length):
line = " "
for category in categories:
if i < len(category.name):
line += f"{category.name[i]} "
else:
line += " "
# print(f"{line} length of line: {len(line)}")
chart += line + '\n'
chart = chart.rstrip()
return chart
food = Category('Food')
clothing = Category('Clothing')
auto = Category('Auto')
food.deposit(700)
food.withdraw(645)
clothing.deposit(300)
clothing.withdraw(255)
auto.deposit(140)
auto.withdraw(100)
print(create_spend_chart([food, clothing, auto]))
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.1 Safari/605.1.15 Ddg/26.1
Challenge Information:
Build a Budget App - Build a Budget App




