Correct output in censor but newlines are added


#1



10. Censor

The output when I ran it if I add some prints to debug is what it should be I believe, but somehow the exercise fails. I think I am not using the split and join functions correctly


def censor(text, word):
    splitted_text=text.split()
    for words in splitted_text:
        if words == word:
            words = "*" * len(words)
            result_text=" ".join(words)
        else:
            result_text=" ".join(words)
    return result_text

censor("this hack is wack hack", "hack")


#2

You type in this line twice thus resulting in extra lines.


#3

you don't seem to understand what .join() does, it joins a list together. It doesn't add (append) a result to a string, for example if i have a list:

date = [4,6,2016]

i can join it:

print "-".join(date)

here is what i would do, create a list, append the items to the list (censored when needed), and then in the end join the list together into a string.


#4

Exactly, the point was to use a list instead of a string to store the words.

def censor(text, word):
    splitted_text=text.split()
    result_text=[]
    for words in splitted_text:
        if words == word:
            words = "*" * len(words)
            result_text.append(words)
        else:
            result_text.append(words)
    return " ".join(result_text)

censor("this hack is wack hack", "hack")

works like a charm