[英]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.