Tell us what’s happening:
My create_spend_chart is failing every test, even the title test. I copy and pasted the exact title from the directions, and my function’s output seems to match the example output exactly. I did create an extra method in the class definition called get_spending, which I call in the printing function. I don’t know if that is somehow throwing off the system.
Your code so far
class Category:
def __init__(self, category):
self.name = category
self.ledger = []
def __str__(self):
string = f'{self.name.center(30, "*")}'
for transaction in self.ledger:
string += f'\n{transaction["description"][:23].ljust(23)}{format(transaction["amount"], ".2f")[:7].rjust(7)}'
string += f'\nTotal: {self.get_balance()}'
return string
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
else:
return False
def get_balance(self):
balance = 0
for transaction in self.ledger:
balance += transaction['amount']
return balance
def get_spending(self):
spending = 0
for transaction in self.ledger:
if transaction['amount'] < 0:
spending += abs(transaction['amount'])
return spending
def transfer(self, amount, other_category):
if self.check_funds(amount):
self.withdraw(amount, f'Transfer to {other_category.name}')
other_category.deposit(amount, f'Transfer from {self.name}')
return True
else:
return False
def check_funds(self, amount):
if amount <= self.get_balance():
return True
else:
return False
def create_spend_chart(categories):
total = 0
for category in categories:
total += category.get_spending()
category_percentages = [
round((category.get_spending() / total) * 10) * 10
for category in categories]
print('Percentage spent by category')
graph_percent = 100
while graph_percent >= 0:
line_string = f'{str(graph_percent).rjust(3)}|'
for percentage in category_percentages:
if percentage >= graph_percent:
line_string += ' o '
else:
line_string += ' '
print(line_string)
graph_percent -= 10
print(f"{' ' * 4}{'-' * (3 * len(categories) + 1)}")
names = [category.name for category in categories]
char_index = 0
max_len = max(len(name) for name in names)
for line in range(max_len):
line_string = ' '
for name in names:
if char_index < len(name):
line_string += f' {name[char_index]} '
else:
line_string += ' '
char_index += 1
print(line_string)
food = Category('Food')
clothing = Category('Clothing')
auto = Category('Auto')
food.deposit(1000, 'Initial transfer')
clothing.deposit(1000, 'Initial transfer')
auto.deposit(1000, 'Initial transfer')
food.withdraw(40)
food.withdraw(10)
clothing.withdraw(33)
auto.withdraw(17)
categories = [food, auto, clothing]
create_spend_chart(categories)
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project