[英]Python recursion returns None
这将非常有趣...给定以下python
代码:
def getBinary(binaryInput, kSize, beginBit):
if int(binaryInput[beginBit + kSize-1])==1:
print 'entered!!!'
shortE = binaryInput[beginBit:kSize+beginBit]
print 'shortE is now: ', shortE
print 'kSize is now: ', kSize
return (shortE,kSize)
else :
print 'else entered...'
kSize -=1
getBinary(binaryInput, kSize, beginBit)
result = getBinary("{0:b}".format(6), 3, 0)
print result
输出为:
else entered...
entered!!!
shortE is now: 11
kSize is now: 2
None
我的意思是因为shortE
为11而kSize
为2,为什么返回值为None
?
当函数结束而不执行return
语句时,它返回None
。 代替
getBinary(binaryInput, kSize, beginBit)
你的意思是
return getBinary(binaryInput, kSize, beginBit)
else
部分缺少代码:
def getBinary(binaryInput, kSize, beginBit):
if int(binaryInput[beginBit + kSize-1])==1:
print 'entered!!!'
shortE = binaryInput[beginBit:kSize+beginBit]
print 'shortE is now: ', shortE
print 'kSize is now: ', kSize
return (shortE,kSize)
else :
print 'else entered...'
kSize -=1
return getBinary(binaryInput, kSize, beginBit)
# ^^^^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.