Discuss / Python / 不使用循环的方法,用到递归

不使用循环的方法,用到递归

Topic source

Pace2cool

#1 Created at ... [Delete] [Delete and Lock User]
def trim(s):
    if s == '':
        return s
    else:
        if s[0] == ' ' or s[-1] == ' ':     # 首或尾有空格
            if s[0] == ' ':        # 首有空格
                s = s[1:]
                if s[-1] == ' ':   # 尾也有空格
                    s = s[:-1]
            else:                  # 仅尾有空格
                s = s[:-1]
            return trim(s)                  # 使用递归,预防首尾有多个空格的情况
        else:
            return s

槿62437

#2 Created at ... [Delete] [Delete and Lock User]

写的好

皇上的黄i

#3 Created at ... [Delete] [Delete and Lock User]

运行失败

孟宪chi

#4 Created at ... [Delete] [Delete and Lock User]

大佬,请收下我的膝盖


  • 1

Reply