CENSOR help


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#


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


I can't even get the replaced astrik word to print...


def censor(text, word):
    text.split(" ")
    c = len(word)
    count = 0
    word_astr = "*" * c
    print word_astr
    
    while count < len(text):
        for i in text:
            if i == word:
                text[count] = word_astr
                count +=1
            else:
                count +=1
    return "".join(text)


#2

Never a good idea to mutate the object you are iterating in a loop. It can lead to issues.

You should not need nested loops for this problem. Try solving with only a single loop.


#3

not getting it :frowning:

def censor(text, word):
    text.split(" ")
    c = len(word)

    word_astr = "*" * c
    
    text.replace(word, word_astr)
    return text

#4

still not getting it.... :frowning:

def censor(text, word):
text.split(" ")

word_astr = "*" * len(word)
new_text = ""
for c in text:
    if c == word:
        new_text= new_text + word_astr
    else:
        new_text = new_text + c
return new_text

#5

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