Budget app __str__ problem

hello everyone, I am currently working on the budget app, and am having some problems in the formatting the list of items part of the project.
Here is my code:
`class Category:

def __init__(self, item):
    self.item = item
    self.ledger = list()
    self.balance = 0


def deposit(self, deposit_amt, deposit_descrpt=None):
    self.deposit_amt = deposit_amt
    self.deposit_descrpt = deposit_descrpt
    self.balance = 0 + deposit_amt
    if self.deposit_descrpt == None:
        self.deposit_descrpt = " "
    x = {"amount": self.deposit_amt, "description": self.deposit_descrpt}
    self.ledger.append(x)


def get_balance(self):
    return self.balance

def check_funds(self, amt):
    self.amt = amt
    if self.amt > self.get_balance():
        return False
    return True

def withdraw(self, withdraw_amt, withdraw_descrpt = None):
    self.withdraw_amt = withdraw_amt
    self.withdraw_descrpt = withdraw_descrpt
    if self.withdraw_descrpt == None:
        y = {"amount": (-1) * self.withdraw_amt, "description": " "}
    else:
        y = {"amount": (-1) * self.withdraw_amt, "description": self.withdraw_descrpt}
    if self.check_funds(self.withdraw_amt):
        self.ledger.append(y)
        self.balance = 0 - self.withdraw_amt
        return True
    return False

def transfer(self, transfer_amt, item2):
    self.transfer_amt = transfer_amt
    self.item2 = item2
    if self.transfer_amt > self.deposit_amt:
        return False
    else:
        x = "Transfer to {}".format(item2)
        self.withdraw(self.transfer_amt, x)
        str(self.ledger.append(self.withdraw(self.transfer_amt, x)))
        z = "Transfer to {}".format(self.item)
        self.deposit(self.transfer_amt, z)
        self.ledger.append(self.deposit(self.transfer_amt, z))
    return True


def __str__(self):
    stars = (int(30 - len(self.item)/2) * "*") + self.item + (int(30 - len(self.item)/2) * "*")
    for dictionary in self.ledger:
        return dictionary.values()

#code included in the main file by FCC:
food = Category(“Food”)
food.deposit(1000, “initial deposit”)
food.withdraw(10.15, “groceries”)
food.withdraw(15.89, “restaurant and more food for dessert”)
print(food.get_balance())
clothing = Category(“Clothing”)
food.transfer(50, clothing)
clothing.withdraw(25.55)
clothing.withdraw(100)
auto = Category(“Auto”)
auto.deposit(1000, “initial deposit”)
auto.withdraw(15)

print(food)
print(clothing)
`

currently my editor is giving me this error:

Traceback (most recent call last): File "/home/beatrix/PycharmProjects3/budjet_app/main.py", line 101, in <module> food.transfer(50, clothing) File "/home/beatrix/PycharmProjects3/budjet_app/main.py", line 77, in transfer x = "Transfer to {}".format(item2) TypeError: __str__ returned non-string (type NoneType)

Now i have done some research online before coming here, and found that this error occurs frequently when one tries to use a “print” statement in the str method (which is not my case) , or when the output in the return statement is not a string. In that case the output should be surrounded by “str()” to fix the problem.

I have tried surrounding both the output of the str method with str() and the line 77 variable pointed out by python with “str()” but none of these two ideas worked. So at this point I am a bit lost for ideas on how to proceed, as nowhere online have I seen a similar problem, Is there something about the str method that I am missing?

Consider what is returned by the __str__ method, when ledger doesn’t contain anything.

@sanity
I understand that if the ledger is empty the str method would return “None” as the list would contain 0 elements.
However I don’t understand how this relates to my problem because in my particular case the list is not empty, right? Sure, clothing does not have a clothing.transfer()
method that has been invoked but it does have other things in the ledger like the withdraw method.

clothing category is instantiated just one line before transfer to it. Then when transfer is being executed line x = "Transfer to {}".format(item2) requires string representation of item2, but it gets None from __str__ method.

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