Tell us what’s happening:
My code is passing every test for the budget app project, except for the very last one that is about returning the chart, but when I look at them I have no clue what the problem is, and I’ve tried editing it around many times. Any assistance is appreciated!
Your code so far
class Category:
categories = []
def __init__(self, category):
self.category = category
self.ledger = []
self.total_spent = 0
Category.categories.append(self)
def __str__(self):
items = []
title_line = self.category.center(30, '*')
for item in self.ledger:
description = item['description'][:23]
amount = "{:.2f}".format(item['amount'])
items.append(f"{description:23}{amount:>7}")
ledger_str = ('\n').join(items)
return f"{title_line}\n{ledger_str}\nTotal: {self.get_balance():.2f}"
def deposit(self, amount, description=''):
self.ledger.append({"amount": amount, "description":description})
def withdraw(self, amount, description=''):
if self.check_funds(amount) == True:
self.ledger.append({"amount": amount * -1, "description": description})
self.total_spent += amount
return True
else:
return False
def get_balance(self):
balance = 0
for entry in self.ledger:
balance += entry['amount']
return balance
def transfer(self, amount, category):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {category.category}")
category.deposit(amount, f"Transfer from {self.category}")
return True
else:
return False
def check_funds(self, amount):
if amount > self.get_balance():
return False
else:
return True
def create_spend_chart(categories):
chart_title = 'Percentage spent by category'
spend_categories = []
result = ''
chart_y = ''
chart_x = ''
for category in Category.categories:
spend_categories.append(category.category)
# calculate percentages
total = sum(category.total_spent for category in categories)
category_percentages = [(category.total_spent / total) * 100 for category in categories]
# y-axis
for i in range(100, -10, -10):
chart_y += f'{i:>3}|'
for percentage in category_percentages:
if percentage >= i:
chart_y += ' o '
else:
chart_y += ' '
chart_y += '\n'
# Horizontal line
chart_y += " " + "-" * (len(Category.categories) * 3 + 1) + "\n"
category_spacing = 2
cat_space = 5
end_space = 6
# vertical categories
for i in range(len(max(spend_categories, key=len))):
for j, category in enumerate(spend_categories):
if i < len(category):
if j == 0:
result += ' ' * cat_space
result += category[i]
else:
result += ' ' * end_space
if j < len(spend_categories) - 1:
result += ' ' * category_spacing
result += '\n'
chart_y += result
# return chart_y
chart_title += '\n' + chart_y
return chart_title
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Challenge Information:
Scientific Computing with Python Projects - Budget App