Script which replaces specific words in text by asterisks (Python)
<p>I tried to create a function which finds a censored word (which is an argument of the function)  in a text (which is a second argument) and replaces all the instances of the word with astrerisks. </p>

<pre><code>def censor(text, word):
    if word in text:
        position = text.index(word)
        new_text = text[0: position] + "*"*len(word) + text[position+len(word):]
        censor(new_text,word)
    else:
        return text


phrase_with = raw_input("Type the sentence")
foo= raw_input("Type the censored word")
print censor(phrase_with, foo)
</code></pre>

<p>But when I call the function inside the function itself it stops working. </p>

<p>And returns "<strong>None</strong>". Why? How can I make it work? </p>

<p>And what are the rules of callling a function within the same function?</p>

<p>You need to return the recursive result like this: </p>

<pre><code>def censor(text, word):
    if word in text:
        position = text.index(word)
        new_text = text[0: position] + "*"*len(word) + text[position+len(word):]
        return censor(new_text,word) # &lt;-- Add return here
    else:
        return text


phrase_with_fuck = raw_input("Type the sentence")
fuck = raw_input("Type the censored word")
print censor(phrase_with_fuck, fuck)
</code></pre>

<p>This happens because the function ends in the <code>if</code> statement, resulting None.</p>

python