10/15 censor


I cant seem to get this right, this is what i put down.
def censor(text,word):
[tab]new = ""
[tab]text = text.split()
[tab]for b in text:
[tab][tab]if o == word:
[tab][tab][tab]return new.append("*" * len(b))
[tab][tab][tab]" ".join(new)
[tab][tab][tab]return new.append(w)
print censor(text,word)

It says that text is not defined. Someone please help.


Can you paste your code with 3 backticks ( which look like this ` ) on both ends?


@drewteriyaki repost your code formatted
To do so you just have to do this


your code goes in between here


or select your code and hit this </> in the tools pane above. No need for the tabs


def censor(text,word):
    new = ""
    text = text.split()
    for b in text:
        if o == word: # What is o doing here?
            return new.append("*" * len(b)) #str objects don't have an 'append'
            " ".join(new)
            return new.append(w) #str objects don't have an 'append'
print censor("This is referenced by text in the function", "and this is word")


I figured it out.

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


This is my solution
def censor(text, word):
bleep = ""
if word in text:
for c in range(len(word)):
bleep += "*"
#print len(word)
text = text.replace(word, bleep)
return text
print censor("Perl er det nye sort", "sort")


My solution in case you still need it.

def censor(text, word):
    splitted = text.split()
    for x in splitted:
        #new_phrase += x
        if x == word:
            splitted[splitted.index(x)] = "*" * len(x)
    return " ".join(splitted)
print censor("this hack is wack hack", "hack")


def censor(text, word):
if word in text:
text = text.replace(word,""len(word))
return text

print (censor("this hack is wack hack", "hack"))



this is my code:

def censor(text,word):
    l = text.split()
    for i in l:
        if l[i] == word:
            del l[i]
            l.append("*" * len(word))
    return " ".join(l)

i get this error message:

Does your censor function take exactly two arguments, a text string and a word to replace with asterisks? Your code threw a "list indices must be integers, not str" error.

But why? Where do I use a str to iterate thourh a list?


In your for loop i is an item in the list you created with l=text.split() and the list is a list of strings. Since the list is a list of strings when your code reaches the if statement if l[i] == word is evaluated as l['this'] (using the example text given of ''this hack is wack hack"). That is why the error occurred regarding list indices must be integers and not strings.

Also the append() method will not work as it adds items to the end of the list.

Try using a list comprehension. I was able to get the whole function down to one line (including return statement).


Thx for the hint with the list comprehensions, I solved it =)

def censor(text,word):
    licomp = text.split()
    licomp = [l.replace(word, '*'*len(word)) for l in licomp]
    return " ".join(licomp)

print censor("i finally did it!","finally")


Let us simplify the .replace method:

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

It's very short isn't it? Let me explain how it works.

.replace method syntax:


A = String that contains the word you want to replace.
B = Word/Sub string to be replaced.
C = Word/Sub string that will replace B.

What does "*"*len(word) do?
"a" * 3 produces "aaa"
"1" * 5 produces "11111"
"-" * 2 produces "--"

In other words it simply generates n number of asterisks based on the length of the variable word.

The text.split() approach is a crude approach however using it is fine. Remember "less is more" but don't go over board.