Python 不断输出“none”
每当我尝试运行此代码时:
def isPalindrome( theSubList ):
theSubListtest = theSubList[0:]
if len(theSubListtest) <= 1:
return True
elif len(theSubListtest) == 2:
x = theSubListtest[0]
y = theSubListtest[1]
if (x == y):
return True
else:
return Falsefirst == theSubListtest.pop(0)
elif len(theSubListtest) > 2:
first = theSubListtest.pop(0)
last = theSubListtest.pop()
if first == last:
isPalindrome(theSubListtest)
else:
return False
candidatePs = [
[1,],
range(8),
range(4) + range(3,-1,-1),
range(4) + [0] + range(3,-1,-1),
range(3) + range(4) + [0] + range(3,-1,-1),
]
for p in candidatePs :
print p, isPalindrome( p )
它对 p 的前两个值正确运行,但对以下三个值输出“None”。非常感谢任何帮助。提前致谢。
Whenever I try to run this code:
def isPalindrome( theSubList ):
theSubListtest = theSubList[0:]
if len(theSubListtest) <= 1:
return True
elif len(theSubListtest) == 2:
x = theSubListtest[0]
y = theSubListtest[1]
if (x == y):
return True
else:
return Falsefirst == theSubListtest.pop(0)
elif len(theSubListtest) > 2:
first = theSubListtest.pop(0)
last = theSubListtest.pop()
if first == last:
isPalindrome(theSubListtest)
else:
return False
candidatePs = [
[1,],
range(8),
range(4) + range(3,-1,-1),
range(4) + [0] + range(3,-1,-1),
range(3) + range(4) + [0] + range(3,-1,-1),
]
for p in candidatePs :
print p, isPalindrome( p )
it runs correctly for the first two values of p but then ouputs "None" for the following three values. Any help is greatly appreciated. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哎呀。
Whoops.
你忘了退货。将这些行:更改
为
,代码将按预期工作。
You forgot a return. Change these lines:
to
and the code will work as expected.