def trim(s):
if s[:1] ==' ':
return trim(s[1:])
elif s[-1:] ==' ':
return trim(s[:-1])
elif (s[:1]==' ' and s[-1:])==' ':
return trim(s[1:-1])
return s
#测试
if trim('hello ') !='hello':
print('测试失败1')
elif trim(' hello') != 'hello':
print('测试失败2')
elif trim(' hello world ') != 'hello world':
print('测试失败3')
elif trim('') !='':
print('测试失败4')
elif trim(' ') !='':
print('测试失败5')
else:
print('测试成功')
Sign in to make a reply
凡九朵
def trim(s):
if s[:1] ==' ':
return trim(s[1:])
elif s[-1:] ==' ':
return trim(s[:-1])
elif (s[:1]==' ' and s[-1:])==' ':
return trim(s[1:-1])
return s
#测试
if trim('hello ') !='hello':
print('测试失败1')
elif trim(' hello') != 'hello':
print('测试失败2')
elif trim(' hello world ') != 'hello world':
print('测试失败3')
elif trim('') !='':
print('测试失败4')
elif trim(' ') !='':
print('测试失败5')
else:
print('测试成功')