Tell us what’s happening:
Hey I’m having a little issue with the last test (percentage bar chart), I don’t see any extra spaces or missing characters and I’m pretty sure the code for the graph is correct.
Your code so far
class Category:
categories_list = []
def __init__(self, category):
self.categories_list.append(category)
self.category = category
self.ledger = []
self.balance = 0
def __str__(self):
if self.ledger:
result = ''
result = str(self.category).center(30, '*') + '\n'
for i,j in enumerate(self.ledger):
if len(self.ledger[i]['description']) <= 23:
description = self.ledger[i]['description'].ljust(23)
else:
description = self.ledger[i]['description'][:23].ljust(23)
if '.' not in str(self.ledger[i]['amount']):
number = str(self.ledger[i]['amount']) + '.00'
if len(number) > 7:
number = number[:7]
else:
number = str(self.ledger[i]['amount'])
if len(number) > 7:
number = number[:7]
result += description + number.rjust(7) + '\n'
result += 'Total: ' + str(self.balance)
return result
return str(self.category)
def deposit(self,amount,description=''):
self.ledger.append({"amount": amount, "description": description})
self.balance += amount
def withdraw(self,amount,description=''):
if self.check_funds(amount):
self.ledger.append({"amount": -(amount), "description": description})
self.balance -= amount
return True
return False
def get_balance(self):
return self.balance
def get_withdrawals(self):
total = 0
for i,j in enumerate(self.ledger):
if self.ledger[i]['amount'] < 0:
total += self.ledger[i]['amount']
return total
def transfer(self, amount, category):
if self.check_funds(amount):
self.ledger.append({'amount': -(amount), 'description': f'Transfer to {category}' })
self.balance -= amount
category.deposit(amount, f'Transfer from {self.category}')
return True
return False
def check_funds(self, amount):
if amount > self.balance:
return False
return True
def percentage(self):
pass
food = Category('Food')
food.deposit(900, 'deposit')
entertainment = Category('Entertainment')
entertainment.deposit(900, 'deposit')
business = Category('Business')
business.deposit(900, 'deposit')
food.withdraw(105.55)
entertainment.withdraw(33.40)
business.withdraw(10.99)
def create_spend_chart(categories):
result = 'Percentage spent by Category\n'
total_withdrawals = 0
longest = [len(category.category) for category in categories]
names = ''
for category in categories:
total_withdrawals += category.get_withdrawals()
for i in range(max(longest)):
line_str = ' '
for category in categories:
if i >= len(category.category):
line_str += ' '
else:
line_str += category.category[i] + ' '
if i != max(longest) - 1:
line_str += '\n'
names += line_str
dashes = ('---' * len(categories) + '-')
for i in range(100,-1,-10):
cat_spaces = ' '
for category in categories:
if category.get_withdrawals() > total_withdrawals:
percent = (category.get_withdrawals() / total_withdrawals) * 1000 // 10
if percent >= i:
cat_spaces += 'o '
else:
cat_spaces += ' '
result += str(i).rjust(3) + '|' + cat_spaces + ('\n')
result += dashes.rjust(len(dashes) + 4) + '\n'
result += names
return result
print(create_spend_chart([business, food, entertainment]))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project