10. censor


#1



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


Oops, try again. Did you create a function called censor? Your code threw a "global name 'censor' is not defined" error.


Please help.


def censor(text,word):
    string.split(text)
    string.split(word)
    for l in word:
        word.replace(*)
        
    " ".join(list)


#2

lets start at the beginning:

string.split(text)

text is the string you want to split, so then you can get: text.split()

i don't see why you would split word

don't use replace, make an empty list, then loop over your splitted text, use if/else to see if word requires censoring and append it to list


#3

Sorry, but what is wrong with this?

def censor(text,word):
    text.split()
    for l in word:
        word = [""]
    for t in text.split():
        if word == [""]:
            text.append("")
        else:
            word.replace("")
        
        " ".join(list)

#4

just make an empty list. then if t (your loop iterator) equals word, append asterisks else append t


#5

I have tried to follow your instructions, but it doesn; work. The error message is:
Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "None" when it should return "*** *** ***"

def censor(text,word):
    text.split()
    list = [""]
    for t in text.split():
        if t == word:
            list.append("*" * len(t))
        else:
            list.append(t)
        
        " ".join(list)

#6

list is a reversed keyword,

after the loop, you need to return the censored string


#7

return list
this isn't working
can i get extra help in codeacademy?


#8

you do join the list into a string before returning, right? And outside/after the loop?


#9

" ".join(list)
    return list

sorry you are explaining well but i have problems with understanding. i have written it like this. it still doesn't work.


#10

you need to store the result of your join action in a variable and return this variable, or combine return and join on a single line


#11

Thanks so much once again! You're really good at explaining its just i struggle A LOT with programming! THANK YOU!:grin:

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

#12

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