Hey everyone,
I’m facing a weird issue that seems to be two-fold.
Here’s a snapshot of my display chart:
Top part is mind. Bottom part is what’s expected.
The issue, which I believes causes the failure, is that I’m displaying an extra ‘o’ in my chart. This happens at row ‘10’ for the business category.
The instructions say to round to the nearest 10th, which is what I’m doing.
When manually calculating it, I get the same result: 10
Of course, the testing is expecting to display a blank space there. This doesn’t make sense to me It also wouldn’t amount to 100% in this approach. The test is displaying enough ‘0’ to amount to 90%. (Entertainment: 20%, Business: 0%, Food: 70%)
What am I missing?
Budget.py:
class Category:
def __init__(self, category):
self.name = category
self.ledger = []
self.total_amount = 0
self.withdraw_amount = 0
self.deposit_amount = 0
def __str__(self):
title_line = 30 - len(self.name)
title_line = title_line / 2
title_line = '*' * int(title_line)
receipt = f'{title_line}{self.name}{title_line}\n'
for item in self.ledger:
item_amount = f"{item.get('amount'):,.2f}"
description = item.get('description')
if len(description) > 23:
description = item.get('description')[0:23]
item_space = 30 - (len(description) + len(item_amount))
item_space_display = ' ' * item_space
receipt += f"{description}{item_space_display}{item_amount}\n"
receipt += f'Total: {self.total_amount}'
return receipt
def check_funds(self, amount):
if amount > self.total_amount:
return False
return True
def deposit(self, amount, description=None):
self.deposit_amount += amount
if description is None:
deposit_item = {"amount": amount, "description": ''}
self.ledger.append(deposit_item)
self.total_amount += amount
else:
deposit_item = {"amount": amount, "description": description}
self.ledger.append(deposit_item)
self.total_amount += amount
def withdraw(self, amount, description=None):
check = self.check_funds(amount)
if check:
self.withdraw_amount += amount
self.total_amount -= amount
if description is None:
withdraw_item = {"amount": -amount, "description": ""}
self.ledger.append(withdraw_item)
return True
else:
withdraw_item = {"amount": -amount, "description": description}
self.ledger.append(withdraw_item)
return True
else:
return False
def transfer(self, amount, category):
check = self.check_funds(amount)
if check:
#update category transfering
self.withdraw_amount += amount
self.total_amount -= amount
withdraw_item = {"amount": -amount, "description": f'Transfer to {category.name}'}
self.ledger.append(withdraw_item)
#update category recieving
deposit_item = {"amount": amount, "description": f'Transfer from {self.name}'}
category.ledger.append(deposit_item)
category.total_amount += amount
category.deposit_amount += amount
return True
else:
return False
def get_balance(self):
balance = self.total_amount
return balance
def create_spend_chart(categories):
chart = f'Percentage spent by category\n'
total_withdraw = 0
category_total_withdraw = {}
y_axis = [0,10,20,30,40,50,60,70,80,90,100]
for category in categories:
total_withdraw += category.withdraw_amount
for category in categories:
category_percent = 100 * round(category.withdraw_amount / total_withdraw, 1)
category_total_withdraw.update({category.name: category_percent})
y_axis.reverse()
for row in y_axis:
if len(str(row)) == 3:
chart += f'{row}| '
elif len(str(row)) == 2:
chart += f' {row}| '
else:
chart += f' {row}| '
for category in categories:
if row <= category_total_withdraw.get(category.name):
chart += f'o '
else:
chart += f' '
chart+= '\n'
chart += ' ----------\n'
top_category_name = 0
#understand how many times to run to fetch all category names
for category in categories:
if len(category.name) > top_category_name:
top_category_name = len(category.name)
x = 0
while x != top_category_name:
chart += ' '
for category in categories:
try:
chart += category.name[x] + ' '
except:
chart += ' '
chart+= '\n'
x += 1
return chart
Here’s the error message when running the tests: