Tell us what’s happening:
According to this I’m missing the last one, but I revised a few times and I got the exact same. Any suggestions? Thanks
Your code so far
class Category:
def __init__(self, cat):
self.cat = cat
self.name = cat
self.ledger = []
return
def deposit(self, amount, description=''):
self.ledger.append({'amount': amount, 'description': description})
return
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):
total = 0
for i in range(len(self.ledger)):
total += float(self.ledger[i]['amount'])
return total
def transfer(self, amount, cat):
if self.check_funds(amount):
text_w = 'Transfer to ' + cat.name
self.withdraw(amount, text_w)
text_d = 'Transfer from ' + self.name
cat.deposit(amount, text_d)
return True
else:
return False
def check_funds(self, amount):
if amount > self.get_balance():
return False
else:
return True
def __str__(self):
text = f'{self.cat:*^30}\n'
for i in range(len(self.ledger)):
descr = f"{self.ledger[i]['description'][:23]}"
text += descr
length = 30 - len(descr)
text += f"{(self.ledger[i]['amount']):.2f}".rjust(length) + f'\n'
text += f'Total: {self.get_balance():.2f}'
return text
def create_spend_chart(categories):
text = f'Percentage spent by category\n'
lines = []
values = []
names = []
length_name = []
displayed_names = []
for category in categories:
values.append(category.get_balance())
names.append(category.name)
length_name.append(len(category.name))
longest_name = max(length_name)
n = 100
total_values = sum(i for i in values)
percentages = [round(i*10 / total_values) for i in values]
for bars in range(11):
lines.append(f'{n:>3}| ')
n -= 10
for qt in percentages:
if qt > (n/10):
lines[bars] += 'o '
else:
lines[bars] += ' '
text += f'{lines[bars]}\n'
text += ' ' + '-'*longest_name + '--'
for i in range(longest_name):
displayed_names.append(' ')
for name in names:
if i < len(name):
displayed_names[i] += f'{name[i]} '
else:
displayed_names[i] += ' '
text += f'\n{displayed_names[i]}'
return text
food = Category('Food')
food.deposit(1000, 'initial deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
auto = Category('Auto')
auto.deposit(2500, 'brrumm brrumm')
auto.withdraw(89.95, 'changing tires')
print(create_spend_chart([food, clothing, auto]))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) 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