这是我遇到的问题,不是返回 new_word 并将其打印出来,而是打印“无”
text = "hey hey hey,hey"
word = 'hey'
def censor(text,word):
new_word = ""
count = 0
if word in text:
latter_count = ((text.index(word))+len(word))
while count < text.index(word):
new_word+= text[count]
count += 1
for i in range(len(word)):
new_word += '*'
while latter_count < len(text) :
new_word += text[latter_count]
latter_count += 1
if word in new_word :
censor(new_word,word)
else :
return new_word
print censor(text,word)
请您参考如下方法:
如果没有 return 语句,函数将返回 None。
可能在进行递归时,if word in text: 为 False,因此没有可返回的内容。您也没有返回递归步骤。您必须返回 censor(new_word,word)
