Python 不断输出“none”

发布于 2024-12-11 06:33:04 字数 867 浏览 1 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

依 靠 2024-12-18 06:33:05

哎呀。

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

Whoops.

if (first == last):
    return isPalindrome(theSubListtest)
else:
    return False
表情可笑 2024-12-18 06:33:05

你忘了退货。将这些行:更改

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

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

,代码将按预期工作。

You forgot a return. Change these lines:

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

to

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

and the code will work as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文