Budget App - Converting instance to a str representation

Tell us what’s happening:
I’m trying to finish the budget app challenge, but i have no clue how to transform the class object into a str like the test want me too.
I Have all the elements inside my class instance, but I have no idea how to convert it into this formated str, that it wants.

Your code so far

class Category:

    num_of_categories = 0

    def __init__(self, name):
        self.name = name
        self.ledger = []
        Category.num_of_categories += 1

    def deposit(self, amount, description=''):
        if len(self.ledger) == 0:
            self.ledger = [{'amount': amount, 'description': description}]
        else:
            self.ledger.append({'amount': amount, 'description': description})

    def withdraw(self, amount, description=''):
        if self.check_funds(amount) == True:
            if len(self.ledger) == 0:
                self.ledger = [{'amount': (-1 * amount), 'description': description}]
            else:
                self.ledger.append({'amount': (-1 * amount), 'description': description})
            return True
        else:
            return False

    def get_balance(self):
        balance = 0
        for line in self.ledger:
            balance = balance + line['amount']
        return balance

    def check_funds(self, amount):
        return amount <= self.get_balance()

    def transfer(self, amount, category):
        if self.check_funds(amount) == True:
            category.deposit(amount, f'Transfer from {str(self.name)}')
            self.withdraw(amount, f'Transfer to {str(category.name)}')
            return True
        else:
            return False

Unittest:

 def test_to_string(self):
        self.food.deposit(900, "deposit")
        self.food.withdraw(45.67, "milk, cereal, eggs, bacon, bread")
        self.food.transfer(20, self.entertainment)
        actual = str(self.food)
        expected = f"*************Food*************\ndeposit                 900.00\nmilk, cereal, eggs, bac -45.67\nTransfer to Entertainme -20.00\nTotal: 834.33"
        self.assertEqual(actual, expected, 'Expected different string representation of object.')

Traceback:

Traceback (most recent call last):
  File "C:\Users\T-Gamer\PycharmProjects\ScientificComputing\Final exam\boilerplate-budget-app\test_module.py", line 83, in test_to_string
    self.assertEqual(actual, expected, 'Expected different string representation of object.')
AssertionError: '<budget.Category object at 0x00000241E29C7100>' != '*************Food*************\ndeposit  [96 chars]4.33'
- <budget.Category object at 0x00000241E29C7100>
+ *************Food*************
deposit                 900.00
milk, cereal, eggs, bac -45.67
Transfer to Entertainme -20.00
Total: 834.33
 : Expected different string representation of object.

Challenge: Budget App

Link to the challenge:

See the def __init__(self) method?
There are a bunch of those for classes which change their behavior when encountering all kinds of commands. They are called magic or dunder methods: Magic or Dunder Methods in Python
The one you are looking for is: def __str__(self)

2 Likes

thank you, it was easyer then a tought.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.