Trouble completing CENSOR


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096

I believe when i join() the text i might be missing one parameter or simply not doing it properly.
I have tried to isolate the issue 'Suspicious part' but i still can't figure it out. I think that i have been exposed to the code for too long and need a new pair of eyes with a different point of view.
I have also run a few test in the python shell by adding a "TEST PRINT" and this is the output:

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



'this **** is a wack hack'

censor("hey hey hey","hey")




''

censor(" ha man","ha")
**
'** man'

censor(" ha man, you are funny ha ha hahaha ha ha","ha")
**
**
**
**
**
'** man, you are funny ** ** hahaha ha ha'

As you may have noticed, i got the the word to be censored right
But when i join them into a text, things get mess up


Replace this line with your code. 

def censor(text,word):
	text_list = text.split()
	censored_text = ""
	for wrd in range(0,len(text_list)):
		if text_list[wrd] == word:
			text_list[wrd] =text_list[wrd].replace(text_list[wrd],"*") * len(text_list[wrd])
			#print(text_list[wrd])#TEST PRINT of text_list[wrd] is CORRECT
		else:
			text_list[wrd] = text_list[wrd]
			print(text_list[wrd]) 
			censored_text = " ".join(text_list) #if am right this will be the "Suspicious"part
	return censored_text


#2

I think this is all you're trying to achieve:

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

print(censor("ha man, you are funny ha ha hahaha ha ha","ha"))

That gives you:

** man, you are funny ** ** ****** ** **

Hope that helps :slight_smile:


#3

I believe it's exactly what the line below in my code do:
text_list[wrd] =text_list[wrd].replace(text_list[wrd],"*") * len(text_list[wrd])
I have isolate the problem and i think something is wrong in my else part with the join().


#4

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