Censor - Oops, try again. Your function fails


#1

Hello,
I get this error Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "*** *** *** " when it should return "*** *** ***".

This is the code that I suppose to work fine:
def censor(text,word):
h = ""
for i in text.split():

    if i ==word:
        h += '*'*len(i)
        h += " "
    else:
        h += i
        h += " "

return h

print censor("hey hey hey","hey")
Is there anyone to help me out with this problem?
Thanks in advance!


Censor
#2

Look for where the unexpected trailing space is getting inserted. That is the problem. The SCT does not expect that last space character.


#3

Thank you for your reply.
You are right I have an unexpected space at the end that I did not pay attention to it before.

Thanks again!


#4

I changed my code, and now it works fine.

def censor(text,word):
h = ""
print len(text.split())
b=0
for i in text.split():
b +=1
if i ==word:
h += ''len(i)
if b < len(text.split()):
h += " "
else:
h += i
if b < len(text.split()):
h += " "
return h


#5

Look it over some more and see if you cannot refactor it a bit. Do not walk away from code just because it works. Make it as simple as can be under the premise that no errant data is present. Assume text is a phrase or sentence, word is a word with no spaces.


#7

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