代码:
def trim(s):
if s == '': # 判断是否没有值
return s
else:
while s != '' and s[-1:] == ' ': # 判断最后一个值是否为空格
s = s[:-1] # 若最后一个值是空格,则切掉最后一个数
while s != '' and s[0] == ' ': # 判断第一个值是否为空格
s = s[1:] # 若第一个值是空格,则切掉第一个数
return s
if trim('hello ') != 'hello':
print('Failure!')
elif trim(' hello') != 'hello':
print('Failure!')
elif trim(' hello ') != 'hello':
print('Failure!')
elif trim(' hello world ') != 'hello world':
print('Failure!')
elif trim('') != '':
print('Failure!')
elif trim(' ') != '':
print('Failure!')
else:
print('Done!')