节目输出中包含"无"...为什么?

0 python function return-value

我搜索了论坛,发现了类似的问题,但没有运气解决我的问题.

我的代码旨在使用递归交换每个单词的每两个字母并打印结果.对于具有偶数字母的单词,输出中包含单词"None",我不知道如何修复...

这是代码:

def encryptLine(line, count):
    headline = line[count:]

    if length(headline) > 0:
        if count == length(line) - 1:
            new = headline
            return new
        elif count <= length(line):
            new = head(tail(headline)) + head(headline)
        new = new + str(encryptLine(line, count+2))
        return new

print(encryptLine('abcd', 0))
Run Code Online (Sandbox Code Playgroud)

'abcd'的输出是badcNone,除了单词None之外,这是正确的.'abcde'的输出是'badce',这是正确的......

在此先感谢您的帮助!

Sve*_*ach 8

添加return ""到函数定义,即

def encryptLine(line, count):
    headline = line[count:]

    if length(headline) > 0:
        if count == length(line) - 1:
            new = headline
            return new
        elif count <= length(line):
            new = head(tail(headline)) + head(headline)
        new = new + str(encryptLine(line, count+2))
        return new
    return ""
Run Code Online (Sandbox Code Playgroud)

否则,None如果不成功,该函数将返回length(headline) > 0.