Censor


#1



Oops, try again. Does your censor function take exactly two arguments, a text string and a word to replace with asterisks? Your code threw a "list indices must be integers, not str" error.

this program is supposed to do the following:
1) split the text string into a list
2) sets variable to legth of the "word" argument
3)creates a loop that says for each element of the list text_split compare that element to "word"and if it is equal then that element of text_split is now the word length multiplied by the "*".
4) at the end of the loop the list "text_split" (along with its new elements) is transformed back into a string and given the variable name "censored".
5) finally i return censored (the modified string)

is the problem here with my logic as described above or the code i have used to implement this logic?


def censor(text, word):
    text_split=text.split()
    word_length=len(word)
    for i in text_split:
        if text_split[i]==word:
            text_split[i]=word_length*"*"
    censored=" ".join(text_split)
    return censored


#2

the problem is here:

for i in text_split:

i will not contain indexes, it will contain the words from text_split

Your code seem to be based on the fact that i are indexes, if you want this use range() in the for loop


#3

    text_split=text.split()
    word_length=len(word)
    for i in text_split:
        if i==word:
            i=word_length*"*"
    censored=" ".join(text_split)
    return censored

#4

so i changed the i so it refers to the entry in the list. but got a different error message...
Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "hey hey hey" when it should return "*** *** ***".


#5

Was this what i suggested?

This won't work, i is the iterator, it gets re-assigned each iteration of the loop, and won't persist anywhere.

Go back to this code:

def censor(text, word):
    text_split=text.split()
    word_length=len(word)
    for i in text_split:
        if text_split[i]==word:
            text_split[i]=word_length*"*"
    censored=" ".join(text_split)
    return censored

and use range()


#6

i dont understand two things
1)why doesnt work.

        if i==word:
            i=word_length*"*"

because it says for every entry in the list text_split take that entry and compare it to "word". if it is the same as word then that entry becomes the length of the word changed into stars. which is what i want to do.
2)if i use the range function i am simply setting the range to the legth of the word. but i dont need to do this because that is what the for loop does. it goes through the whole range of something. so why do i need range()?

what am i missing here in these 2 cases?


#7

but i only exist inside the loop, changes made to i won't persist, as you can see when running this code:

aList = ["hey","hey","hey"]

for i in aList:
  print "before:", i
  if i == "hey":
    i = "***"
  print "after:", i

i simply gets re-assigned each iteration of the loop.

You want to use range() so you have indexes to substitutes words (which requires censoring) in text_split, which is what you do here:

text_split[i]=word_length*"*"

if you want to make changes to a list (which you want), i should be an index, to achieve this we use range


#8

so in my program does the "i" in "for i in text_split", and the "i" in the line "text_split[i]=word_length*"*" mean two different things as i have written them? i.e. the first "i" is referring to the thing which occupies the space and the second "i" refers to the position number only. so i have mixed two different concepts but named them the same thing. if that's the case then i understand where i have gone wrong


#10

yes, for i in text_split means that i will contain words from the list

what you do here refers to position number, or indexes as they are called.

You can do this, but like i have been trying to tell you, you need to use range() in your for loop to get the indexes....


#11

I'm having trouble understanding this, and this discussion didn't quite help, but did bring up my question. I don't understand why this only works with indexes. I was trying:

def censor(text, word):
   censtxt = text.split()
   for x in censtxt:
       if x == word:
           x = "*" * len(word)
   return " ".join(censtxt)

But it only worked when I used indexes:

def censor(text, word):
   censtxt = text.split()
   for i in range(len(censtxt)):
       if censtxt[i] == word:
           censtxt[i] = "*" * len(word)
   return " ".join(censtxt)

As far as I understand it so far, the first one says "for every item in the list, if it is identical to the string in word, then replace it," whereas the second says, "for every index in the list, if the item in that index is identical to the string in word, then replace it." What am I missing here that makes the first one fail?


#12

sorry, how did this not help you?

If you use range, which gives you indexes, which you use to manipulate the list.


#13

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