Censor


#1

I'm trying to do the censor function, but it wont return to me the whole text, only the word that is censored out. Why doesn't it loop through all the words in the text? What can I do to this code to make it work?


https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#

def censor(text,word):
text=text.split()
for c in text:
    if c==word:
        c="*"*len(c)
    else:
        c==c
return c 
text="".join(text)
return text

when I run the code it gives me this error message.

Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "" when it should return " *** ***".

Replace this line with your code.


#2

Lots of indentation issues.

Consider using a variable other than text so the original input is preserved.

words = text.split()

Create an empty list to store your intermediate results.

result = []
for w in words:
    if w ==  word:
        result.append( ... )
   else:
        result.append(w)
return " ".join(result)

#3

Create a new list that appends c. After that, you join the new list.
This is what i used:

def censor(text,word):
    texspl = text.split()
    texnew = []
    for a in texspl:
        if word == a:
            a = "*" * len(word)
        texnew.append(a)
    return " ".join(texnew)

#4

Censor can be done 4 lines:

def censor(text,word):
string = ""
string = text.replace(word,"*" * len(word))
return string


#5

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