python两个while循环?
def trim(s):
if len(s)==0:
return s
else:
while s[0:1]==' ':
if s[0:1]!=' ':
break
s=s[1:]
print(s)
#return s 这里不能加一个return 如果加了,会把第一个while的结果返回
# 这个结果就不能继续运行第二个while循环了
while s[-1:]==' ':
if s[-1:]!=' ':
break
s=s[:-1]
return s
return s
# 测试:
if trim('hello ') != 'hello':
print('测试失败1!')
elif trim(' hello') != 'hello':
print('测试失败2!')
elif trim(' hello ') != 'hello':
print('测试失败3!')
elif trim(' hello world ') != 'hello world':
print('测试失败4!')
elif trim('') != '':
print('测试失败5!')
elif trim(' ') != '':
print('测试失败6!')
else:
print('测试成功!')
Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有