Tell us what’s happening:
Cases 19, 20, 22, & 24 are flagged as errors.
My main issue is calculating percentages to be rounded up to the nearest 10, I think from there everything else will be an easier fix.
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.balance = 0
def deposit(self, amount, description = ''):
records = {'amount': amount, 'description': description}
self.balance += amount
self.ledger.append(records)
def withdraw(self, amount, description = ''):
if self.check_funds(amount):
amount = amount * -1
self.balance += amount
records = {'amount': amount, 'description': description}
self.ledger.append(records)
return True
else:
return False
def get_balance(self):
return self.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):
if self.balance < amount:
return False
else:
return True
def __str__(self):
ledger_title = self.name.center(30, "*")
ledger_sections = ''
for entry in self.ledger:
descriptions = entry['description']
amounts = entry['amount']
if len(descriptions) > 23:
descriptions = descriptions[0:23]
amounts = f'{amounts:.2f}'
descriptions = f'{descriptions:.23}'
ledger_sections += f'{descriptions:23}{amounts:>7}\n'
return f'{ledger_title}\n{ledger_sections}Total: {self.balance:.2f}'
def create_spend_chart(categories):
total_spent = 0
category_total = []
category_amount = len(categories)
percentages = []
chart_title = 'Percentage spent by category\n'
chart = ''
graph = ''
names = ''
for category in categories:
temp_total = 0
for entry in category.ledger:
if entry['amount'] < 0:
total_spent += abs(entry['amount'])
temp_total += abs(entry['amount'])
category_total.append(temp_total)
percentages = [round(num / total_spent * 10) * 10 for num in category_total]
for i in range(100, -10, -10):
for _ in range(category_amount):
if percentages[_] == i:
graph += 'o '
chart += f'{i:3}| {graph}\n'
if i == 0:
chart += ' ' + ('-' * (category_amount * 3)) + '-'
max_chars = max(len(category.name) for category in categories)
for i in range(0, max_chars):
names += " "
for category in categories:
if len(category.name) > i:
names += category.name[i] + " "
else:
names += " "
names += "\n"
return f'{chart_title}{chart}\n{names}'
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:149.0) Gecko/20100101 Firefox/149.0
Challenge Information:
Build a Budget App - Build a Budget App