Tell us what’s happening:
The test to round down the a categories’ spending percentages to the nearest ten fails to pass. Any assistance will be highly appreciated.
Your code so far
class Category:
total_withdrawn = 0
def __init__(self, name):
self.name = name
self.ledger = []
self.balance = 0
self.withdrawn = 0
def deposit(self, amount, description=''):
'''Deposit an amount into the categoory's balance.'''
self.balance += amount
self.ledger.append({'amount': amount, 'description': description})
def withdraw(self, amount, description=''):
'''Withdraw an amount from the category's balance.'''
if not self.check_funds(amount):
return False
self.balance -= amount
self.withdrawn += amount
Category.total_withdrawn += amount
self.ledger.append({'amount': -amount, 'description': description})
return True
def check_funds(self, amount):
'''Validate whether the balance is sufficient for the transaction.'''
if self.balance >= amount:
return True
return False
def get_balance(self):
'''return the current balance of the Category'''
return self.balance
def transfer(self, amount, other):
'''Transfer an amount from one category to another.'''
if self.withdraw(amount, f'Transfer to {other.name}'):
other.deposit(amount, f'Transfer from {self.name}')
return True
else:
return False
def __str__(self):
'''returns a formatted string representation of an object.'''
output = '{:*^30}\n'.format(self.name)
for transaction in self.ledger:
amount, description = transaction.values()
if len(description) > 23:
description = description[:23]
formatted_description = '{:<23}'.format(description)
formatted_amount = '{:>7.2f}'.format(amount)
output += '{}{}\n'.format(formatted_description, formatted_amount)
output += f"Total: {'{:.2f}'.format(self.get_balance())}"
return output
def create_spend_chart(categories):
'''returns a formatted string of a chart representing the spending percentage of each object'''
spend_chart = 'Percentage spent by category'
for y_value in range(100, -1, -10):
line = f'\n{"{:>3}".format(y_value)}| '
for category in categories:
if (((category.withdrawn / Category.total_withdrawn) * 100) // 10) * 10 >= y_value:
line += 'o '
else:
line += ' '
spend_chart += line
horizontal_line = '---' * len(categories) + '-'
spend_chart += f'\n{f"{{:>{4+len(horizontal_line)}}}".format(horizontal_line)}'
for index in range(max([len(category.name) for category in categories])):
line = f'\n '
for category in categories:
if len(category.name) > index:
line += f'{category.name[index]} '
else:
line += ' '
spend_chart += line
return spend_chart
'''
food = Category('Food')
food.deposit(1000, 'deposit')
food.withdraw(112.15, 'groceries')
food.withdraw(13.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
clothing.deposit(400, 'deposit')
clothing.withdraw(134.15, 'shirts')
clothing.withdraw(44, 'trousers')
auto = Category('Auto')
auto.deposit(3000, 'deposit')
auto.withdraw(40.15, 'oil')
auto.withdraw(300.89, 'tires and bearings')
food.transfer(23, clothing)
food.transfer(100, auto)
clothing.transfer(50, food)
clothing.transfer(34.4, auto)
auto. transfer(14, food)
auto.transfer(53, clothing)
print(food)
print(clothing)
print(auto)
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/143.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App - Build a Budget App
