recursion within a class
I am trying to place a recursive formula inside a class statement
class SomeNode:
def __init__(self, a):
leng = len(a)
half= leng/2
self.firstnode=a[0][0]
开发者_运维问答 self.child1=SomeNode([a[i]for k in range(leng)])
self.child2=SomeNode([a[j] for j in range(leng)])
def recursfunc(self):
print self.firstnode
recursfunc(self.child1)
recursfunc(self.child2)
However I keep getting the error message NameError: global name 'recursfunc' is not defined
You need to use self.recursfunc()
def tri_recursion(k):
if(k>0):
result = k+tri_recursion(k-1)
print(result)
else:
result = 0
return result
print("\n\nRecursion Example Results")
tri_recursion(7)
精彩评论