Censor Exercise (Practice Makes Perfect 10.)


#1



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


No error message is given, but the code returns "None".


The code is meant to replace a specific censored word with asterisks.

I would appreciate any assistance, thanks!


Replace this line with your code. 

def censor(text, word):
    text_list = text.split()
    word_list = word.split()
    word_length = len(word)
    for x in text_list:
        while x in word_list:
            x = "*" * (word_length)
    "".join(text_list)
    return text_list

print censor("hello all", "all") #testing


#2

you join the list into a string here:

"".join(text_list)

but you don't store the result of this join action anywhere?


#3

Fixed that, thanks! The code still returns the original text though (in this example "helloall").


#4

x contains read only items from the list. modifying x won't modify the list

if you want to modify the list, use range or enumerate to get indexes and manipulate the list


#5

The following code still returns the same result. Am I correctly using enumerate?

def censor(text, word):
    text_list = text.split()
    word_list = word.split()
    word_length = len(word)
    for n, x in enumerate(text_list):
        while x == word_list:
            x = "*" * (word_length)
    p = "".join(text_list)
    return p

print censor("hello all", "all") #testing

#6

n contains the indexes, which you can use as indexes to censor words in text_list

you don't need a word list. word should simply be and stay a string

Please keep thinking while i help you trouble shoot the problem


#7

I solved the Censor Exercise a different way, but I am still unsure of how to use enumerate() to solve the above exercise. I changed uses of word_list to word and tried multiple combinations of the n,x format. What am I missing? (thanks again for your continued help)


#8

well, n gives you indexes, so we can use that to update our text_list:

for n, x in enumerate(text_list):
    if x == word:
        word_list[n] = "*" * (word_length)

#9

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