Discuss / Python / 测试通过,小白自己写的算是第一个小程序 开心

测试通过,小白自己写的算是第一个小程序 开心

Topic source

宽人宽语

#1 Created at ... [Delete] [Delete and Lock User]

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('测试成功!')


  • 1

Reply