BudgetApp: def transfer(self, amount, destination) issues

Am working on the python BudgetApp and am looking at the TRANSFER process:

accepts an amount and another budget category as arguments. The method should add a withdrawal with the amount and the description "Transfer to [Destination Budget Category]". The method should then add a deposit to the other budget category with the amount and the description "Transfer from [Source Budget Category]". If there are not enough funds, nothing should be added to either ledgers. This method should return True if the transfer took place, and False otherwise.

I am 90% certain I am on the right track with the code (all of the code so far is):

class Category():

  def __init__(self, name):
      self.name=name
      self.ledger=list()
      self.runningBalance=int()
  
  def deposit(self,amount,description=""):
    self.ledger.append({"amount":amount,"description":description})
    
    self.runningBalance=self.runningBalance+amount
    print(self.name, "deposit:", self.ledger)

  def withdraw(self, amount, description=""):
    # amount passed in should be stored in the ledger as a negative number. If there are not enough funds, nothing should be added to the ledger. This method should return True if the withdrawal took place, and False otherwise.
    
    if self.runningBalance >= amount:
      self.ledger.append({"amount":amount*-1,"description":description[0]})
      
      self.runningBalance=self.runningBalance-amount
      print(self.name, "withdrawl:", self.ledger)

      return True
    else:
      return False

  def get_balance(self):
    #returns the current balance of the budget category based on the deposits and withdrawals that have occurred.
    return (self.runningBalance*10)/10

  def transfer(self, amount, destination):
    #accepts an amount and another budget category as arguments. The method should add a withdrawal with the amount and the description "Transfer to [Destination Budget Category]". The method should then add a deposit to the other budget category with the amount and the description "Transfer from [Source Budget Category]". If there are not enough funds, nothing should be added to either ledgers. This method should return True if the transfer took place, and False otherwise.

    if self.runningBalance >= amount:
      print(amount)
      self.ledger.append({"amount":amount*-1,"description": "Transfer to " + destination})
      
      destination.deposit(amount, "Transfer from "+ self.name)

      self.runningBalance=self.runningBalance-amount
      print(self.name, "transfer:", self.ledger)

  def check_funds(self, amount):
    #accepts an amount as an argument. It returns False if the amount is less than the balance of the budget category and returns True otherwise. This method should be used by both the withdraw method and transfer method.
    print(amount)

def create_spend_chart(categories):
  return

but I am getting the error:

Traceback (most recent call last):
  File "main.py", line 13, in <module>
    food.transfer(50, clothing)
  File "/home/runner/fcc-budget-app/budget.py", line 36, in transfer
    self.ledger.append({"amount":amount*-1,"description": "Transfer to " + destination})
TypeError: can only concatenate str (not "Category") to str

am fairly sure it is how I am accessing the value of destination but, having looked at other code for the same example I cannot get mine to work. Maybe I have missed something further up…

thanks as always

looking in more detail. Transfer is called by:

food.transfer(50, clothing)

so obviously the second parameter isn’t a string - so how do I get it’s value to do things with?

sorted… i needed “destination.name” so it looks at the attributes of the class named in destination

I need help in this transfer method this is my whole code but I get weird outputs I don’t know why

class Category:

   def __init__(self,budget_category):

       self.ledger = []

       self.budget_category = budget_category

       self.call = False

   def deposit( self, amount, description = ''):

       self.amount = amount

       self.description = description

       if self.call:

           Category(self.anthr_category).ledger.append({"amount": self.amount, "description": self.description})

           return None

       self.ledger.append({"amount": self.amount, "description": self.description})

   def withdraw(self , withdraw_amount , description = ''):

       self.withdraw_amount = withdraw_amount

       self.description = description

       if self.withdraw_amount <= self.get_balance(): 

           self.ledger.append({"amount": -self.withdraw_amount, "description": self.description})    

       return self.check_funds(self.withdraw_amount)

   def get_balance(self):

       balance = 0

       for item in range(len(self.ledger)):

           balance += self.ledger[item]["amount"]

       return balance

   def transfer(self, trans_amount , anthr_category):

       self.anthr_category = anthr_category

       self.trans_amount = trans_amount

       self.call = self.check_funds(self.trans_amount)

       if self.call:

           self.withdraw(self.trans_amount , f"Transfer to {self.anthr_category}")

           self.deposit(self.trans_amount , f"Transfer from {self.budget_category}")

       return self.call

   def check_funds(self , amount):

       self.amount = amount

       if self.amount > self.get_balance():

           return False

       return True

   def _string(self):

       self.header = self.budget_category.center(30 , '*')

       self.lst = ''

       for item in range(len(self.ledger)):

           self.short_descrptn = self.ledger[item]["description"][:23]

           self.cash = f'{self.ledger[item]["amount"]:.2f}'.rjust(30-(len(self.short_descrptn)))

           self.lst += f'{self.short_descrptn}{self.cash}' + '\n'

       self.lst = self.lst + f'Total: {self.get_balance()}'

       return self.header + '\n'+ self.lst

   def __str__(self):

       return self._string()

when I run the test module I get weird astericks on the test_transfer method in class unittests as follows `======================================================================
FAIL: test_to_string (main.UnitTests)

Traceback (most recent call last):
File “c:\Users\Bradleys\Desktop\python projects\boilerplate-budget-app\test_module.py”, line 83, in test_to_string
self.assertEqual(actual, expected, ‘Expected different string representation of object.’)
AssertionError: ‘[75 chars]ggs, bac -45.67\nTransfer to ********Ent -20.00\nTotal: 834.33’ != '[75 chars]ggs, bac -45.67\nTransfer to Entertainme -20.00\nTotal: 834.33’
Food
deposit 900.00
milk, cereal, eggs, bac -45.67

  • Transfer to ********Ent -20.00
  • Transfer to Entertainme -20.00
    Total: 834.33 : Expected different string representation of object.

======================================================================
FAIL: test_transfer (main.UnitTests)

Traceback (most recent call last):
File “c:\Users\Bradleys\Desktop\python projects\boilerplate-budget-app\test_module.py”, line 52, in test_transfer
self.assertEqual(actual, expected, ‘Expected transfer method to create a specific ledger item in food object.’)
AssertionError: {‘amo[18 chars]ption’: ‘Transfer to Entertainment*\nTotal: 0’} != {‘amo[18 chars]ption’: ‘Transfer to Entertainment’}

  • {‘amount’: -20, ‘description’: ‘Transfer to Entertainment’}
  • {‘amount’: -20,
  • ‘description’: ‘Transfer to Entertainment*\nTotal: 0’} : Expected transfer method to create a specific ledger item in food object.

Ran 11 tests in 0.008s

FAILED (failures=3)`