Budget App FAIL: test_to_string

I am receiving an assertion error in test module.

AssertionError: '****[23 chars]***\n\ndeposit                 900.00\nmilk, c[66 chars]4.33' != '****[23 chars]***\ndeposit                 900.00\nmilk, cer[64 chars]4.33'
  *************Food*************
  -
  deposit                 900.00
  milk, cereal, eggs, bac -45.67
  Transfer to Entertainme -20.00
  Total: 834.33 : Expected different string representation of object.

my code so far.

class Category():
    def __init__(self,name,):
        self.amount_dpt = 0.0
        self.amount_wdt = 0.0
        self.amount_trf = 00.0
        self.name = name
        self.ledger = list()
        self.balance = 00.0
    def deposit(self, amount,description = ''):
        self.amount_dpt += float(amount)
        self.ledger.append({'amount' : self.amount_dpt, 'description' : description})
        self.balance += float(amount)
    def withdraw(self,amount,description=''):
        if amount < self.balance:
            self.balance -= float(amount)
            self.ledger.append({'amount' : - amount, 'description' : description})
            self.amount_wdt += amount 
            return True 
        else:
            return False
    def get_balance(self):
        return self.balance
    def transfer(self,amount,name):
        if float(amount) > self.balance:
            return False
        else:
            self.ledger.append({'amount' : -amount, 'description' :f'Transfer to {name.name}'})
            name.ledger.append({'amount' : amount, 'description' :f'Transfer from {self.name}'})
            self.balance -= float(amount)
            name.balance += float(amount)
            return True
    def check_funds(self,amount):
        if float(amount) > self.balance:
            return False
        else:
            return True
    def __str__(self):
        nm =  f"{self.name:*^30}\n"
        wholeText = list()
        for item in self.ledger:
            amt = str(item["amount"])
            des = item["description"]
            if not "." in amt:
                num = f'{amt}.00'
            else:
                digits = amt.split(".")
                decimals = digits [1]
                figures = digits [0]
                if len(decimals)>2:
                    decimals = decimals[0:2]
                    num = f'{figures}.{decimals}'
                elif len(decimals)==1:
                    decimals = f'{decimals}0'
                num = f'{figures}.{decimals}'
            if len(num)>7:
                num = num[-7:-1]
            label = des
            if len(label)>23:
                label = label[0:23]
            lenSpace = 30 - len(label) - len(num)
            space = f'{lenSpace*" "}'
            wholeText.append(f'{label}{space}{num}')
        total = f'Total: {self.balance}'
        text = "\n".join(wholeText)
        whole = [nm, text, total]
        output = "\n".join(whole)
        return output

****[23 chars]***\n\ndeposit 900.00\nmilk, c[66 chars]4.33
****[23 chars]***\ndeposit 900.00\nmilk, cer[64 chars]4.33

You have an extra \n before deposit

1 Like

Thank you . It worked

1 Like

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