def trim(s): while ' ' in s[-1] or s[0]: if ' ' in s[-1]: s = s[0:] if ' ' in s[0]: s = s[:-1] return s
if trim('hello ') != 'hello': print('测试失败!') elif trim(' hello') != 'hello': print('测试失败!') elif trim(' hello ') != 'hello': print('测试失败!') elif trim('') != '': print('测试失败!') elif trim(' ') != '': print('测试失败!') else: print('测试成功!') 为什么运行之后就没反应了
Sign in to make a reply
摩羯座的KSCN
def trim(s): while ' ' in s[-1] or s[0]: if ' ' in s[-1]: s = s[0:] if ' ' in s[0]: s = s[:-1] return s
测试:
if trim('hello ') != 'hello': print('测试失败!') elif trim(' hello') != 'hello': print('测试失败!') elif trim(' hello ') != 'hello': print('测试失败!') elif trim('') != '': print('测试失败!') elif trim(' ') != '': print('测试失败!') else: print('测试成功!') 为什么运行之后就没反应了