Tell us what’s happening:
I have revised my code with multiple sources and it is printing the exact thing needed in the terminal but for some reason error 19 and 20 are still showing up. I know that the other errors I still have to fix but error 19 and 20 are not working.
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
return False
def get_balance(self):
balance = 0
for item in self.ledger:
balance += item['amount']
return 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 True
else:
return False
def __str__(self):
output = self.name.center(30, '*') + '\n'
for item in self.ledger:
description = item['description'][:23]
amount = f"{item['amount']:.2f}"
output += f"{description:<23}{amount:>7}\n"
output += f"Total: {self.get_balance():.2f}"
return output
def create_spend_chart(categories):
total = 0
percentages = []
for category in categories:
for item in category.ledger:
if item['amount'] < 0:
total += abs(item['amount'])
for category in categories:
category_total = 0
for item in category.ledger:
if item['amount'] < 0:
category_total += abs(item['amount'])
percentage = (category_total / total) * 100
print(percentage)
percentage = round(percentage // 10) * 10
percentages.append(percentage)
output = 'Percentage spent by category\n'
for level in range(100, -1, -10):
output += f'{level:>3}|'
for percentage in percentages:
if percentage >= level:
output += ' o '
#else:
#output += ' '
output += '\n'
return output
food = Category('Food')
bob = Category('Bob')
cat = Category('Cat')
food.deposit(900, 'deposit')
food.withdraw(45, 'milk, cereal, eggs, bacon, bread')
food.transfer(50, bob)
bob.withdraw(30, 'lobia')
cat.deposit(100,'photography')
cat.withdraw(18,'animal')
categories = [food, bob, cat]
#print(food.get_balance())
#print(food.check_funds(400))
#print(food)
#print(bob)
print(create_spend_chart(categories))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36 Edg/151.0.0.0
Challenge Information:
Build a Budget App - Build a Budget App