Python Budget-App: test_get_balance fail

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

Hello~!

It looks like you are defining the get_balance method to print the entire ledger? The get_balance method should only return Total: and the total.

1 Like

Thanks for the tip, back to coding.

1 Like

There actually is a small mistake in the test. If you look at

*!= 854.33 : Expected balance to be `54.33`*

That string is not representative of the test’s expected value (854.33). I got confused for a bit with that myself since I failed that test at first.

I was banging my head wondering how can that possibly equal 54.33?? Is that why I am failing??

The value is fine but that string can be misleading. It should be:

**!= 854.33 : Expected balance to be `854.33`**

In my case my problem was returning the number as a string when a float is expected to be returned from calling the method.

I hope this is useful for anyone who comes here looking for an answer to my problem.