def trim(s): if s[0]==' ' and s[-1]==' ': s=s[1:-1] trim(s) elif s[0]==' ': s=s[1:] trim(s) elif s[-1]==' ': s=s[:-1] trim(s) else: return s
if len(s) == 0: return '' if s[0] == ' ' and s[-1] == ' ': s = s[1:-1] return trim(s) elif s[0] == ' ': s = s[1:] return trim(s) elif s[-1] == ' ': s = s[:-1] return trim(s) else: return s
这样也不对啊,提示s[1:]是无效的语法
Sign in to make a reply
bigbigbigbook
def trim(s): if s[0]==' ' and s[-1]==' ': s=s[1:-1] trim(s) elif s[0]==' ': s=s[1:] trim(s) elif s[-1]==' ': s=s[:-1] trim(s) else: return s