def trim(s):
if(len(s)==0): return s if(s[0]==' '): s=s[1:] s=trim(s) if(len(s)==0): return s if(s[-1]==' '): s=s[:-1] s= trim(s) print('-',s,'-') return s
if trim('hello ') != 'hello': print('测试失败!') elif trim(' hello') != 'hello': print('测试失败!') elif trim(' hello ') != 'hello': print('测试失败!') elif trim(' hello world ') != 'hello world': print('测试失败!') elif trim('') != '': print('测试失败!') elif trim(' ') != '': print('测试失败!') else: print('测试成功!')
大佬思路好清晰,膜拜一波
Sign in to make a reply
xlwb_Mew
-- coding: utf-8 --
def trim(s):
测试:
if trim('hello ') != 'hello': print('测试失败!') elif trim(' hello') != 'hello': print('测试失败!') elif trim(' hello ') != 'hello': print('测试失败!') elif trim(' hello world ') != 'hello world': print('测试失败!') elif trim('') != '': print('测试失败!') elif trim(' ') != '': print('测试失败!') else: print('测试成功!')