Censor


#1

code:

def censor(text,word):
    newtxt=""
    ln=len(word)
    for words in text:
        if words==word:
                newtxt=newtxt+ "*" * ln
        else:
            newtxt=newtxt+words
    return " ".join(newtxt)

error:
Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "h e y h e y h e y" when it should return "*** *** ***".
`


#2

help plz........................................


#3

here:

for words in text:

text is a string, looping over a string will cause go letter by letter. so then this:

if words==word:

can never be true, a letter can never equal a word (unless the word is one letter, but very unlikely)

here:

return " ".join(newtxt)

newtxt is already a string, why join it? .join() is to join a list into a string


#4

def censor (text, word):

"if words==word:"

It should be like this
if words == word:
u did not follow indentation for this code


#5

i don't see a difference?


#6

Check the hint of that exercise. It recommends using string.split() and " ".join(list). I don't think those methods are covered in the previous exercises. You can see how they work here: https://www.tutorialspoint.com/python/python_strings.htm


#7

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