10 - Censor


I tried this code:

text = "please censor me"
word = "censor"

def censor(text,word):
    wordlist = text.split()
    for each,n in wordlist:
        if each == word:
            wordlist[n] = "*" * len(word)
    return ' '.join(wordlist)

But this error showed up: "Your code looks a bit off--it threw a "too many values to unpack" error. Check the Hint if you need help!" I don't know what that means or how to solve it...

My version of censor!

it has something to do with the each,n. I'm not positive, but I don't think you can pass two bits of information into a loop like that.


Ok, so I solved it like this:

text = "please censor me"
word = "censor"

def censor(text,word):
    wordlist = text.split()
    for i in range(0,len(wordlist)):
        if wordlist[i] == word:
            wordlist[i] = "*" * len(word)
    return ' '.join(wordlist)


Thanks! That was the issue

My version of censor!

No problem glad I could help :slight_smile:


Your answer is great! Thanks for that really short and understanable code.


my code without using split(). Check if it is fine for all cases.

def censor(text, word):
for c in text:
if c==' ':
if w==word:
w='*' * len(word)
t=t+w+' '
t=t+w+' '
if w==word:
w='*' * len(word)
return t


Could any one plz help y this code is not working

def censor(t,w):
for i in a:
print i
if (i==w):
i ="*" * l
print i
s=" ".join(i)
print s
return s
censor("hey hey hey","hey")


This line is one of the problems.

But also why your code has more than one error is because the variables and arguments you have passed are so similar and small that it is very easy to get confused.


So try passing legible arguments and variables and you should start to understand what your code needs to do better.


Tnx for ur help. i understood.. i am changing the word but thats not reflecting in the list.


I didn't mean change the word the way you've written your code is what is resulting in an error.


Look at the way he wrote his code and observe the differences between this one and yours.


oops sry for the confusion.. what I really meant is that i understood my error. i am fetching the data in the list in a temporary variable and i am changing oly in the temp variable not in the list. that's why i didnt get the answer.


Oh haha now I understand! :sweat_smile:


Found this neat little trick:

def censor(text, word):
star = 0
if word in text:
star = len(word) * ('*')
return str.replace(text, word,star)


Nice, its so much more concise when using the built in function :). In this case you don't need the if statement (the replace method takes care of it for you). You could make it even shorter with this:

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


This is a bit complicated. keeping it simple will help keep it smooth.

"Simple is smooth, smooth is power!" haha. here is what I came up with:

def censor(text,word):

return text.replace(word,""len(word))

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