I’m very confused with tasks 17+ on the Budget App. I seem to be getting more or less the right answer, but I can’t get a pass, and I can’t figure out why None keeps printing. Any help would be greatly appreciated!!
Preformatted text
class Category:
def __init__(self, name):
self.ledger = []
self.name = name
self.balance = 0
def deposit(self, amount, descr = ''):
self.balance += amount
self.ledger.append({'amount': amount, 'description': descr})
def withdraw(self, amount, descr = ''):
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': descr})
self.balance -= amount
return True
return False
def get_balance(self):
return self.balance
def transfer(self, amount, instance):
if self.check_funds(amount):
self.withdraw(amount, f'Transfer to {instance.name}')
instance.deposit(amount, f'Transfer from {self.name}')
return True
return False
def check_funds(self, amount):
if amount > self.get_balance():
return False
return True
def __str__(self):
asterisks = ((30 - len(self.name)) // 2) * '*'
header = f'{asterisks}{self.name}{asterisks}\n'
lines = ''
for mini_dict in self.ledger:
space = 30
descr = mini_dict['description']
amount = "{:.2f}".format(mini_dict['amount'])
written_descr = descr[:23]
space -= len(written_descr)
space -= len(amount)
lines += written_descr + (space * ' ') + amount + '\n'
last_line = f'Total: {self.balance}'
return(header + lines + last_line)
def print_bubbles(list_of_percentages, number, line):
for percentage in list_of_percentages:
if percentage >= number:
line += 'o '
else:
line += ' '
return(line)
def create_spend_chart(categories):
total_balance = 0
list_of_percentages = []
for category in categories:
total_balance += category.balance
for category in categories:
balance_percentage = round((category.balance / total_balance), 1) * 100
list_of_percentages.append(balance_percentage)
print('Percentage spent by category')
for i in range(100, -10, -10):
txt = '{:>3}'
print(print_bubbles(list_of_percentages, i, f'{txt.format(i)}| '))
print(' -' + '-' * 3 * len(categories))
longest_name = 0
for category in categories:
if len(category.name) > longest_name:
longest_name = len(category.name)
else:
continue
for i in range(longest_name):
line = ' '
for category in categories:
if i < len(category.name):
line += f"{category.name[i]} "
else:
line += " "
print(line)
food = Category('Food')
clothing = Category('Clothing')
auto = Category('Auto')
food.deposit(1000, 'deposit')
food.deposit(900, 'deposit')
food.withdraw(45.67, 'milk, cereal, eggs, bacon, bread')
food.withdraw(10.15,'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
food.transfer(50,clothing)
auto.deposit(15000,'initial deposit')
food.transfer(10,auto)
clothing.withdraw(40,'new shirts')
auto.withdraw(80,'car repair')
accounts = [food,clothing,auto]
print(create_spend_chart(accounts))