10. censor - what is wrong with this code?


#1



I wrote this code for the censoring practice, but it is a bit off and I cannot really understand, why.

def censor(text,word):
csillag= "*" * len(word)
text = text.split()

for i in text:
    if i==word:
        i=csillag
    return i

text=" ".join(text)
return text


Your function fails on censor("hey hey hey","hey").
It returns "" when it should return " *** ***".
(I could not paste it right, but it returns 1 "***" instead of 3)


If I remove the "return i" part, it does nothing. I have not really get why the for loop does not go through the whole text.

Thanks for your help!


Replace this line with your code.


#2

You might have to give split a delimiter. (Tell it to split the string up by spaces). I also thought you might like to see a cleaner way to do it.

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

#3

Deliminiter did not worked.
I know that something is wrong with the for loop, I just cannot get why.

Your solution is a pretty elegant one.


#4

I think I found the solution and some explanation:

for i in range(len(text)):
    if text[i]==word:
        text[i]=csillag

And thanks for stetim94 who solved this problem:


#5

My style of coding is a little bit different from yours, but I made your code work. Also, I don't think there's a need to return i from the for loop.

def censor(text,word):
	split_text = text.split()
	new_text = ""
	
	for i in split_text:
	    if i==word:
	        new_text = new_text + ("*" * len(word)) + " "
	    else:
	        new_text = new_text + i + " "
	
	return new_text
	
print(censor("this is test test test text testing lololol", "test"))

#6

@tekkie1618 answers does not explain why your code is not working.

the problem is in your for loop, i gets assigned each word (from text) in turn, we can see this:

for i in text:
    print i

which means, each run of the loop i gets re-assigned. and after the loop i will simply hold the last value.

Either way, what you do here:

i = csillag

doesn't persist.

How to overcome this problem? You can do two things, create an empty list, and append the censored or uncensored word, by using if/else. Then in the end use .join() to join the list into a string.

In the for loop, use range() which gives you indexes, which you can use the manipulate the list stored in text, which means you can censor the words when needed, and then in the end change the list into a string with .join()

Also, a function ends the moment a return keyword is reached, so the return should be after the loop


#7

Thank you for the help!
Now I am starting to get the logic in this.


#8

Yea, the logic is more important, of course you can build in function like replace(), but that comes later, first understand the logic, do ask if you have any questions


#9

this code:

def censor(text,word):
	split_text = text.split()
	new_text = ""
	
	for i in split_text:
	    if i==word:
	        new_text = new_text + ("*" * len(word)) + " "
	    else:
	        new_text = new_text + i + " "
	
	return new_text
	
print(censor("this is test test test text testing lololol", "test"))

won't work, you have a additional space at the end of the sentence, of course you can slice it off.


#10

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