Error in program-censor(codecademy)-Practice makes perfect
<p>I tried executing this code but in the output the last <em>word-hack</em> is not turning into <em>asterisk</em> and is not even shown.  </p>

<p>Enter code here:</p>

<pre><code>def censor(text,word):
    w=""
    text1=""
    for i in text:
        if i==" ":
            if w==word:
                text1=text1+"*"*len(word)
            else:
                text1=text1+w
            text1=text1+" "
            w=""
        else:
            w=w+i
    return text1
 print censor("this hack is wack hack", "hack")
</code></pre>

<p>The error is happening in the fourth line of the function. If you add an extra space to the end of "text" in your example, you get it it print what you want. Without digging too much into why the problem is happening, the following will work.</p>

<pre><code>def censor(text,word):
    text = text + " "
    w=""
    text1=""
    for i in text:
        if i==" ":
            if w==word:
                text1=text1+"*"*len(word)
            else:
                text1=text1+w
            text1=text1+" "
            w=""
        else:
            w=w+i
    return text1

print(censor("this hack is wack hack ", "hack"))
</code></pre>

python