10. Censor


#1



The code returns the original text without altering it and I'm not sure why.


I would expect that the beginning of the word to be censored will be replaced with *


def censor(text, word):
    x = text
    text = " "
    for i in range(len(x)):
        var = x[i:i + len(word)]
        if word in var == True:
            text += "*" * len(word)
        else:
            text += x[i]
    return text


#2

After completing some of the later exercises in the section I realised my mistake was in the line:

If I understand it correctly I'm essentially stating if True == True which is a cause for disaster. I went back and fixed the code and edited it so that it complies with the instructions, here's what I ended up with.

def censor(text, word):
    x = text
    y = -10
    text = ""
    for i in range(len(x)):
        var = x[i:i + len(word)]
        if word in var:
            text += "*" * len(word)
            y = i
        elif y < i < y + len(word):
            text = text
        else:
            text += x[i]
    return text

Also, if I'm understanding the if var in word == True line incorrectly I would appreciate if someone let me know, thanks :slight_smile:


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.