10 - Censor


#1

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!
#2

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.


#3

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)

#4

Thanks! That was the issue


My version of censor!
#5

No problem glad I could help :slight_smile:


#6

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


#7

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


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

#8

Could any one plz help y this code is not working

def censor(t,w):
a=t.split()
l=len(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")


#9

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.


#10

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


#11

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


#12

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


#13

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


#14

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.


#15

Oh haha now I understand! :sweat_smile:


#17

Found this neat little trick:

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


#18

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))


#19

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")