Discuss / Python / 费了好长时间才弄好

费了好长时间才弄好

Topic source

-- coding: utf-8 --

def trim(s): a=len(s) if a==0: return '' end=len(s)-1 star=0

while star<=end:
    if not s[star]==' ':
        print('star',star)
        break
    else:
        star=star+1
        if star>end:
            return ''
while end>=0:
    print(end,s[end])
    if not s[end]==' ':
        print(star,end)
        print('切片结果',s[star:end+1])
        return s[star:end+1]        
    else:
        end=end-1

测试:

if trim('hello ') != 'hello': print('1 测试失败!') elif trim(' hello') != 'hello': print('2 测试失败!') elif trim(' hello ') != 'hello': print('3 测试失败!') elif trim('') != '': print('4 测试失败!') elif trim(' ') != '': print('5 测试失败!') else: print('测试成功!')


  • 1

Reply