10.Censor - It only works on one word


#1



10.Censor


only censors one word and none after that


censor all the words that match the keyword


def censor(text, word):
    inc = 0
    star = "*" * len(word)
    text = text.split()
    
    while(inc < len(word)):
        if(text[inc] == word):
            text[inc] = star
        inc += 1
    
    text = " ".join(text)
    
    print text
    return text
    
    
censor("some thing to say thats some stuff", "some")
censor("Yo go fro yo go","go")


#2

In your while loop it looks like it will only repeat for the length of word not the length of the list of words, text. Maybe try swapping out word for text in your while loop repeat conditions.
For example:
While(inc < len(text)):

Hope that helps.


#3

Hey, try doing this:

Make a new list and append the words to it. If it is the word to be censored, append the stars to it.

It will look like this:

def censor(text, word):
    star = "*" * len(word)
    word_list = text.split()
    new_word = []
    for item in word_list:
        if item == word:
            new_word.append(star)
        else:
            new_word.append(item)
    return " ".join(new_word)

This works perfect for me. I hope this helps you too. :smile:


#4

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