Censor


#1




I can't seem to figure this out.


def censor(text, word):
    text = text.split(word)
    split_index = 1
    for split in text:
        text.append("*" * len(word))
        split_index +=2
    new_text = " ".join(text)
    return new_text


#2

string refers to the string you want to split, in this case the string you want to split is stored in the variable text


#4

you still have a question?


#5

Yes, I can't figure out my updated code, now.


#6

you are appending to what you are looping over? That will cause an infinity loop.

i would make a empty list, use if/else to determine if the word requires censoring, if so append * times length word to the list else append without censoring


#7

def censor(text, word):
    text = text.split(word)
    new_text = []
    length = len(word)
    replacement = "*" * length
    for w in text:
        if w == word:
            new_text.append(replacement)
        else:
            new_text.append(w)
    text = " ".join(new_text)
    return text

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


#8

here is your code code with function call + additional print:

def censor(text, word):
    text = text.split(word)
    print text
    new_text = []
    length = len(word)
    replacement = "*" * length
    for w in text:
        if w == word:
            new_text.append(replacement)
        else:
            new_text.append(w)
    text =  " ".join(new_text)
    return text
print censor("hey hey hey","hey")

see this print:

print text

what does this tell you?


#9

def censor(text, word):
    text = text.split(word)
    print text
    new_text = []
    for w in text:
        if w == word:
            length = len(word)
            w = "*" * length + " "
            new_text.append(w)
            print new_text
        else:
            new_text.append(w)
    text = " ".join(new_text)
    return text

I don't know why nothing prints. I can't see what anything prints.


#10

did you include the function call (i provided it for you)? Without the function call the function won't execute, thus the print statement won't be reached, nothing is printed


#11

Okay, I see now. But why does .split() do that? I do not comprehend the function of .split().


#12

thankfully there are docs: python docs - str.split()

in short, between the parentheses is what you use as separator


#13

def censor(text, word):
    text1 = text.split(word)
    new_text = []
    for w in text1:
        if w == text:
            length = len(word)
            w = "*" * length
            new_text.append(w)
            print new_text
        else:
            new_text.append(w)
    text = " ".join(new_text)
    return text

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

I really can't figure this out.


#14

so you use word as separator? this means every time word is found, the string is split into the list, don't you want to use another separator to split each word in text?

did you read the documentation?


#16

Thank you! It works!


#17