廖雪峰切片课后题

来源:互联网 发布:淘宝差评被卖家骚扰 编辑:程序博客网 时间:2024/05/21 05:17
def trim(str):
    i=0;j=-1;
    if(len(str)==0):
        return str;
    else:
        while(True):
            if str[i]==' ':
                i+=1;
                if(i==len(str)):  
                    i=0;
                    break;
            else:
                break;
        while(True):
            if str[j]==' ':
                j-=1;
                if((-j-1)==len(str)):
                    j=0;
                    break;
            else:
                j=j+len(str)+1;
                break;
        str=str[i:j];   
        return str;
if trim('hello  ') != 'hello':
    print('测试失败!')
elif trim('  hello') != 'hello':
    print('测试失败!')
elif trim('  hello  ') != 'hello':
    print('测试失败!')
elif trim('') != '':
    print('测试失败!')
elif trim('    ') != '':
    print('测试失败!')
else:
    print('测试成功!')
原创粉丝点击