Discuss / Python / 测试

测试

Topic source

def trim2(s): n=0 e=0; for str in s: if str==' ': n=n+1 else: break for e in range(len(s)): if s[-(e+1)]==' ': e=e+1 else: break print('e:',e,' n:',n,'---',s[n:len(s)-e]) return s[n:len(s)-e]

def trim(s): if(len(s)>0): if s[0]==' 'or s[-1]==' ': if s[0] == ' ': s=s[1:] if len(s)>0 and s[-1]==' ': s=s[:-1] if (len(s) > 0): if s[0] == ' ' or s[-1] == ' ': s=trim(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('测试失败!')

修改成5个空格

elif trim(' ') != '': print('测试失败!') else: print('测试成功!')


  • 1

Reply