def trim(s):
n=1
k = -1
while s[:n] == ' ':
n=n+1
s=s[n:len(s)]
while s[k:] == ' ':
k=k-1
s=s[:k]
return s
if trim('hello ') != 'hello':
print('测试失败!')
elif trim(' hello') != 'hello':
elif trim(' hello ') != 'hello':
elif trim(' hello world ') != 'hello world':
elif trim('') != '':
elif trim(' ') != '':
else:
print('测试成功!')
Sign in to make a reply
宽人宽语
def trim(s):
n=1
k = -1
while s[:n] == ' ':
n=n+1
s=s[n:len(s)]
while s[k:] == ' ':
k=k-1
s=s[:k]
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('测试成功!')