Censor


#1



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


My code is only cycling once instead of looping it.


def censor(text, word): 
    text=text.split()
    new_text=[]
    for i in text:
        if i == word : 
            new_text= "*" * len(i)
        else:
            new_text+=i
            
    return new_text


#2

You're Re-assigning the list again.
hint*
Append would work good here,
also the censor word ( It's always "*" * len(i) ) is constant so define it outside of loop as constant string value.

Concatenation operator works for same data type.

hint**
Use append() method

At last you need to convert the list back to string.


#3

Okay so I'm trying to convert the list and it does it using this code:
return " ".join(new_text)
but then it gives me the error
--> Oops, try again. Your function fails on censor("Yo go fro yo go","go"). It returns "Y o ** f r o y o **" when it should return "Yo ** fro yo **".

I'm not sure how to get rid of the space between the letters. This is the updated code.
def censor(text, word):
text=text.split()
constant = "*" * len(word)
new_text=[]
for i in text:
if i == word :
new_text.append(constant)
else:
new_text+=i
return " ".join(new_text)


#4

As mentioned earlier,just append i to new_text list.


#5

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