Python属于If,但返回不工作

2024-10-02 14:22:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我只是想做一个简单的hackerrrank question。我想出的代码打印出了答案,但没有返回。我不知道为什么

def myjump(c,j):
    if len(c) <= 1:
        print("answer: ",j)
        return j
    elif len(c) > 2:
        if c[2] == 0:
            c = c[2:]
        else:
            c = c[1:]
    else:
        c = c[1:]

    j += 1
    myjump(c,j)

    return "Test"

def jumpingOnClouds(c):
    if len(c) <= 1:
        return 0

    res = myjump(c,0)
    print("return: ",res)
    return res

result = jumpingOnClouds([0,0,1,0,0,1,0])

输出:

('answer: ', 4)
('return: ', 'Test')

我不明白为什么代码似乎落入了“if len(c)<;=1:”块中并打印数字,但没有命中返回值


Tags: 答案代码answertestlenreturnifdef