Censor


#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 *** " when it should return " *** ***". >


def censor (text, word) :
    new=''
    new = text.split()
    for string in new:
        if string == word:
            cenword = "*" * len(string)
            new.remove(string)
            new.append(cenword)
    text = " ".join(new)  
    return text
    print text


#thanks in advance for your help


#2

what if you have this:

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

your solution would mean the word order in the sentence doesn't stay intact

if you want to replace in new list, use range() which will give you indexes, so you can keep the word order intact


#3

thanks for the reminder, is it possible for me to work it out without involving range()? how?


#4

that is also possible, make an new list, in the loop use if condition to check if words needs censoring, if so, append asterisks to the new list, else append without censoring


#5

def censor (text, word) :
    new=''
    new = text.split()
    for i in new:
        new_list = []
        if i == word:
            new_list = "*" * len(i)
            new.append(new_list)
        else:
            new.append(new_list)
    text = " ".join(new)  
    return text
    print text

my new codes after your instructions, i am not sure if i followed your instructions to the letter

am getting this error...Oops, try again. Your code looks a bit off--it threw a "" error. Check the Hint if you need help!


#6

new_list should be outside the loop, its the list containing our censored string. Placing it inside the loop will make a new empty list every run of the loop, not remembering what we append to it in earlier loop iterations

then we loop over new, so i now contains a word from the sentence. Now we want to check if i equals word, if so, censor it by appending asterisks to new_list

else, append i to new_list (i contains word from the sentence which doesn't need censoring)


#7

ef censor (text, word) :
    new=''
    new = text.split()
    new_list = []
    for i in new:
        if i == word:
            new_list.append("*" * len(i))
        else:
            new_list.append(i)
    text = " ".join(new)  
    return text
    print text


#i did as you instructed me to, but i get this error, Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "hey hey hey" when it should return "*** *** ***".

#Thanks for help

#8

here:

text = " ".join(new)

why join new? new_list contains your censored list, which you need to join into a string


#9

text = " ".join(new)
#Thanks so much, i realized where my error was...you are actually an expert .

#10

do you understand the solution you coded? :slight_smile:


#12

actually i am not yet sure if i understand the solution of my code, now that you guided me through it, can you give me a vivid understanding of the solution as well? am just a beginner:blush:


#13

But you coded it with my guidance?

we create a new_list to store the result.

then we loop over the sentence split into words, we use if condition to determine if the word needs censoring, if so, we append asterisks to to the new_list

else, we just append the word (loop iterator)

after the for loop has finished, new_list is a list containing our censored sentence, then we use join to join the list into a string, which we return


#14

i got it sir/madam, stay blessed . Thanks


#15

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