I’m having a problem passing (test_get_balance). I noticed there’s a typo in the test and was wondering if there may be a bug (854.33 mentioned once and then 54.33 listed in the error message in test_module.py lines 43-44).
My get_balance seems to print okay:
*************Food*************
deposit 900.00
milk, cereal, eggs, bac -45.67
Total: 854.33
Here is the error I get:
FAIL: test_get_balance (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/fcc-budget-app/test_module.py", line 45, in test_get_balance
self.assertEqual(actual, expected, 'Expected balance to be 54.33')
AssertionError: '*************Food*************\ndeposit [65 chars]4.33' != 854.33 : Expected balance to be 54.33
My Code is below (it’s not complete for this assignment yet):
class Category:
def __init__(self, category):
self.category = category
self.ledger = []
def deposit(self, amount, description=''):
self.ledger.append({'amount':amount, 'description':description})
def withdraw(self, amount, description=''):
if self.check_funds(amount) == True:
self.ledger.append({'amount':amount*-1, 'description':description})
return True
else:
return False
def transfer(self, amount, category):
if self.check_funds(amount) == True:
#transfer out of category
withdrawDescription = "Transfer to " + category.category
self.ledger.append({'amount':amount*-1, 'description':withdrawDescription})
#transfer into category
depositDescription = "Transfer from " + self.category
category.deposit(amount, depositDescription)
return True
else:
return False
def get_balance(self):
result = ''
title = self.category.center(30, '*')
result += title + '\n'
total = 0
for row in self.ledger:
#for key in row:
description = row['description'][:23]
result += description
amount = row['amount']
total += row['amount']
amount = format(amount, '.2f')
offset = 30 - len(description)
offsetStr = '{:>' + str(offset) + '}'
amount = offsetStr.format(amount)
result += amount + '\n'
result += 'Total: ' + format(total)
return result
def check_funds(self, amount):
balance = 0
for row in self.ledger:
for key in row:
balance += row['amount']
if amount < balance:
return True
else:
return False
def create_spend_chart(categories):
pass #to be completed later
Am I doing something wrong, or does there happen to be a bug here? I just started learning Python this week so am not feeling super confident.
Thanks