为什么这段PYTHON程序的输出为None?

def delete(word_list):

if word_list:

    for i in word_list:

        if i[0] == 'stop':

            word_list.remove(i)

print delete([('noun', 'bear'),('stop', 'the')])

请各位帮忙看看,多谢

2个回答

delete函数没有返回值,所以是None。改成这样:

def delete(word_list):
    if word_list:
        for i in word_list:
            if i[0] == 'stop':
                word_list.remove(i)
        return word_list
print delete([('noun', 'bear'),('stop', 'the')])

def delete(word_list):
if word_list:
for i in word_list:
for j in range(len(i)):
if i[j] == 'stop':
word_list.remove(i)
return word_list

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问