Tell us what’s happening:
I’m trying to make sense of the console output for the tests, I’m on the final function and it seems there is either an extra space or a missing space that is being hidden at ends of lines perhaps, but either way I can’t figure out how to ascertain that from the console output.
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
else:
return False
def get_balance(self):
balance = 0
for transaction in self.ledger:
balance += transaction['amount']
return balance
def transfer(self, amount, category):
if self.check_funds(amount):
self.withdraw(amount, f'Transfer to {category.name}')
category.deposit(amount, f'Transfer from {self.name}')
return True
else:
return False
def check_funds(self, amount):
return amount <= self.get_balance()
def __str__(self):
line_length = 30
output = ''
for i in range((line_length - len(self.name)) // 2):
output += '*'
line_length -= 1
output += f'{self.name}'
line_length -= len(self.name)
for i in range(line_length):
output += '*'
output += '\n'
for transaction in self.ledger:
spaces = 30
if len(transaction['description']) > 23:
output += f"{transaction['description'][0:23]}"
spaces -= 23
else:
output += f"{transaction['description']}"
spaces -= len(transaction['description'])
spaces -= len(f"{float(transaction['amount']):.2f}")
for i in range(spaces):
output += ' '
output += f"{float(transaction['amount']):.2f}"
output += '\n'
output += f"Total: {float(self.get_balance()):.2f}"
return output
def create_spend_chart(categories):
percentages = []
total = 0
for category in categories:
spent = 0
for item in category.ledger:
if item['amount'] < 0:
total += abs(item['amount'])
spent += abs(item['amount'])
percentages.append({'name': category.name, 'spent': spent})
for item in percentages:
item['percentage'] = item['spent'] * 100 // total
output = ''
output += 'Percentage spent by category\n'
i = 100
while i >= 0:
output += f"{' ' if i < 100 else ''}{' ' if i == 0 else ''}{i}|"
for item in percentages:
if item['percentage'] >= i:
output += ' o '
else:
output += ' '
output += ' \n'
i -= 10
output += ' '
for i in range(len(percentages)):
output += '---'
output += '-\n'
names = []
for item in percentages:
names.append(len(item['name']))
for i in range(max(names)):
output += (' ')
for item in percentages:
output += (f" {item['name'][i] if i < len(item['name']) else ' '} ")
output += '\n'
return output
# setUp (line 12, VM382)
food = Category("Food")
entertainment = Category("Entertainment")
business = Category("Business")
# test_create_spend_chart(line 17, VM382)
food.deposit(900, "deposit")
entertainment.deposit(900, "deposit")
business.deposit(900, "deposit")
food.withdraw(105.55)
entertainment.withdraw(33.40)
business.withdraw(10.99)
print(create_spend_chart([business, food, entertainment]))
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project