10/15 censor. I need help


#1

def censor(text, word):
for x in text.split():
for n in word.split():
if n == x:
return text.replace(x,"*"*len(n))

print censor(“hey hey hey”,“hey”)
print censor(“this hack is wack hack”, “is”)
print censor(“this hack is wack hack”, “hack”)

*** *** ***
th** hack ** wack hack
this **** is wack ****

error message is this.

Oops, try again. Your function fails on censor(“hey hey hey”,“hey”).
It returns “*** *** ***” when it should return *** hey hey

I research this error all day. but I don’t know…
I want to next step. help me.

thank you. it’s korean versison.


#2

Hello @gigacoder05452,
Can you indent your code properly?


#4

I solved myself.

def censor (text,word):
result = []
count = 0
for x in text.split():
if word in x:
if count == 0:
result.append("*" * len(word))
count += 1
else:
result.append(x)
else:
result.append(x)
return " ".join(result)

print censor(“hey hey hey”,“hey”)

why not used replace?

Another Sample is used replace…
Korean version is wierd.


#5

replace() is dull, it allows for a single line solution. And look at line 8, this hack is wack hack gets censored to th** hack ** wack hack, this shouldn’t get partial censored

Also, the course you currently taking is in the old learning environment, which is no longer supported, please update to supported course:

https://www.codecademy.com/learn/learn-python


#6

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