Discuss / Python / 大家帮我看下哪里出错了

大家帮我看下哪里出错了

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('测试成功!') 为什么运行之后就没反应了


  • 1

Reply