Need help with Budget App

Hey guys,

I have been working on the Budget app and I am stuck for some time now. I want to get the {amount} and {description} from the self.ledger and put them on a new string inside the repr function. That way, when the user calls print(category) it displays according to the challenge’s requirement.

Here’s my code so far:

class category:
    def __init__(self,name):
        self.name = name
        self.ledger = []  
        self.amounts =[]

    def __repr__(self):

        n = int((30 - len(self.name))/2)       
        num_asterisks = '*' * n    
        Headline = num_asterisks+self.name+num_asterisks+'\n'

        for i in self.ledger:
            description_index = i.index(description)
            transaction = i[description_index:]
 def deposit (self, amount, description=''):
                    
        self.amounts.append(amount)
        self.ledger.append(f'amount:{amount}, description:{description}')
        
        return True

 def withdraw (self, amount, description=''): 
       
        if self.check_funds(amount):
            self.ledger.append(f'amount:{abs(amount)}, description:{description}')
            self.amounts.append(amount*-1)
            
            return True
        else:
            return False       

def get_balance(self):
      
        balance = sum(self.amounts)
        return balance
      
    def transfer (self, amount, other_category): 
        if self.check_funds(amount):
            self.withdraw(amount, 'Transfer to'+ other_category.name)
            other_category.deposit(amount,'Transfer from'+self.name)
            return True
        else: 
            return False

    def check_funds(self,amount):
      
        if amount > self.get_balance():
            return False
        else:
            return True

So, the indentation is off in this pasted code and it won’t parse.

Additionally, when using the double underscore methods, you need to choose the correct one. While print() will call obj.__repr__() eventually, it only does so after trying obj.__str__(), which is what you should be targeting here. __repr__() is the preferred method for return the reproduction of an object via the repr() function, which is a specialized string representation not necessarily meant for user consumption (it’s really for reconstituting an object).

Right now it looks like this function does not return a string or print or anything. Since you have the specs, what should it return? Make it return something and then compare your results to the expected ones and then proceed from there. If you have questions, feel free to ask but at this point I’m not sure where your difficulties lie.

1 Like