# 10.Censor need some help

#1

I've tried again and again and i still don't know how to solve this problem.Following is my code.

``````def censor(text,word):
a=text.split(' ')
result=[]
for b in range(len(a)):
if a[b]!=word:
result+=a[b]
else:
result+=('*'*len(word))
b=result.split(' ')
return ' '.join(b)``````

#2

i like how you use split, which i think is a good solution. Either way, then your for loop:

``for b in range(len(a)):``

why use range? You can just do:

``````for b in a:
print b``````

which will give you each word. which means you can simple append each word to result array:

``````if b != word:
result.append(b)``````

you can think how your else should be. `+=` is used for adding something to a string, you have a list.

Then you just join the result:

``return ' '.join(result)``

i don't understand, why you split it here:

``b=result.split(' ')``

result is still a list

#3

what's the function of these codes?

``````for b in a:
print b``````

this is my manuscript before and following is my last edition

``````def censor(text,word):
a=text.split(' ')
result=[]
for b in range(len(a)):
if a[b]!=word:
result+=a[b]
else:
result+=('*'*len(word))
return ' '.join(b)``````

#4

yea, the biggest problem is the use of `+=`, instead of append.

Well, you can save yourself a lot of trouble by using: `for b in a`, and the `print b` was to demonstrate how much easier this loop is, and how it works

#5

``````for b in a:
print b``````

here is my real final edition

``````def censor(text,word):
a=text.split(' ')
result=[]
for b in a:
if b!=word:
result.append(b)
else:
result.append(len(word)*'*')
return ' '.join(result)``````

#6

``````for b in a:
print b``````

yea, this was just to demonstrate how the loop works, instead of using range, which is more complicated in this case. Looking at the code, i would say issue resolved?

#7
`def censor(text, word):    mask = ""    for x in range(len(word)):      mask = mask + "*"    return text.replace(word, mask)`

• define function censor, with parameters text (to be censored) and word (to censor)

• initialise a variable, mask as a string

• set up a for-loop with length equal to the variable word, which we will censor

• for each iteration of the for-loop add an asterisk to our mask string, to match the length of the word string

• return the text string, while using the replace function with arguments of word (to be replaced) and mask (to replace it)