10. Censor


#1

Why is my code printing both the censored version of the text argument and the non-censored version? It should print only the censored input. Sorry if this code is a mess; I am not very good. Thank you for any help.

def censor(text,word):#defines variables as strings and takes input and moves to a list
    inp=str(text)
    remo=str(word)
    transient=inp.split(" ")  
    for i in range(len(transient)):#check each item in transient
        if transient[i]==remo:
             transient.append("*"*len(remo))
        else:
             transient.append(transient[i])
    return " ".join(transient)

       
print censor("oh look a goldfish","goldfish")

#2

your appending to the list which contains the original splitted string, this will give you the original string + censored version in one list

two options, append to a new list or replace words which require censoring (we can update elements in list using indexes)


#3

Thank you. I created a new list and it now works.


#4