Censor


#1

def censor(text, word):
    text = text.split()
    for i in text:
        if i==word:
            length = len(word)
            i = "*" * length
    text =" ".join(text)
    return text
censor("hey hey hey", "hey")

I cant figure out what is wrong. Please help!

`


#2

i gets assigned each word assigned in turn:

def censor(text, word):
    text = text.split()
    for i in text:
        print i
censor("hey hey hey", "hey")

so i won't persist, and get overwritten every run of the loop

You have two options (well more i guess, but this are the simplest):

  1. create a new empty list and append the words to it, censoring when needs be
  2. use range() which gives you indexes, allowing you to replace words in text if they require censuring.

#3

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