Censor


#1

Hello all, I am facing some problems with this code:

def censor(text, word):
....ltext_list = text.split(" ")
....word_len = len(word)
....stars = "*" * word_len
....for i in range(len(text_list)):
........if text_list[i] == word:
............text_list[i] = stars
....return " ".join(text_list)

The return message is: Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "*** hey hey" when it should return "*** *** ***".

Thank you for your help.


#2

this line:

ltext_list = text.split(" ")

you called it ltext_list, why not simply text_list? Then it should work, this code doesn't even run


#3

that was a mistake in copy paste here, even without the l, it doesn't work.


#4

can you post an updated version of your code with markup:

This way you only have to copy paste


#6

def censor(text, word):
    text_list = text.split(" ")
    word_len = len(word)
    stars = "*" * word_len
    for i in range(len(text_list)):
        if text_list[i] == word:
            text_list[i] = stars
        return " ".join(text_list)

not working with backticks

def censor(text, word):
....text_list = text.split(" ")
....word_len = len(word)
....stars = "*" * word_len
....for i in range(len(text_list)):
........if text_list[i] == word:
............text_list[i] = stars
return " ".join(text_list)


#7

the return is wrongly indented, a function ends the moment a return keyword is reached, in your case the first run of the loop

put return at the same indent level as for loop, so the whole loop can run

use the triple backticks, not the single one, those are for inline


#8

hey, I have already solved it, but thank you for your help


#9