I have function
a call function
a), afterwards function
a can return the result to be printed.
class C: ... def a(self, data): p = self.head return self.b( p,data) def b(self, p, data): current = p if current.data == data: return True else: return False if __name__=="__main__": x = C() print(x.a(1))
Sometimes it returns None eventhough it is intended to be
True. I'm not sure what happened?
This question is related to
It's impossible to say without seeing your actual code. Likely the reason is a code path through your function that doesn't execute a
return statement. When the code goes down that path, the function ends with no value returned, and so returns
Updated: It sounds like your code looks like this:
def b(self, p, data): current = p if current.data == data: return True elif current.data == 1: return False else: self.b(current.next, data)
That else clause is your
None path. You need to return the value that the recursive call returns:
else: return self.b(current.next, data)
BTW: using recursion for iterative programs like this is not a good idea in Python. Use iteration instead. Also, you have no clear termination condition.