九州体育网 - 十年信誉网站

关于作者

Discuss / Python / 想知道我错在哪里

想知道我错在哪里

Topic source

Cher懿

#1 Created at ...

-- coding: utf-8 --

def trim(s): L=[] L.append(s) n=0
while L[n]: n=n+1 if L[n]==' ': L.pop(n) return L

测试:

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('测试胜利!')

ZQ丶LEO

#2 Created at ...

你这样用POP()会把中间的空格也删掉,注意题目

Gonathan佳

#3 Created at ...

这样子删除所有空格了 ,可以加个限制只判断前后空格


  • 1

Reply

WARNING: You are using an old browser that does not support HTML5. Please choose a modern browser (Chrome / Microsoft Edge / Firefox / Sarafi) to get a good experience.