【问题标题】:Python keeps ouputting "none"Python不断输出“无”
【发布时间】:2011-12-13 18:42:30
【问题描述】:

每当我尝试运行此代码时:

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 的前两个值运行正确,但随后对以下三个值输出“无”。任何帮助是极大的赞赏。提前致谢。

【问题讨论】:

  • def isPalindrome(x): return x == x[::-1]

标签: python boolean python-2.7 palindrome


【解决方案1】:

哎呀。

if (first == last):
    return isPalindrome(theSubListtest)
else:
    return False

【讨论】:

    【解决方案2】:

    您忘记了退货。更改这些行:

    if (first == last):
        isPalindrome(theSubListtest)
    

    if (first == last):
        return isPalindrome(theSubListtest)
    

    并且代码将按预期工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      • 2012-10-27
      相关资源
      最近更新 更多