Censor


#1

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

Oops, try again. Your function fails on censor("Yo go fro yo go","go"). It returns "Yo" when it should return "Yo ** fro yo **".

I've been playing around with my code but I am seriously stuck


#2

a function ends the moment a return keyword is reached

if a return keyword is reached in a loop, the function needs to end, so the loop breaks

so your loop breaks in its first iteration

using replace() is so dull, it allows for a single line solution. I would advise against using it at the moment


#3

If we look at your code line by line, there are a number of problems
where it's not doing quite what you think it is:

def censor(text, word):
    new_word = text.split(" ")
    # now new_word is an array of strings

    for i in new_word:
        # this section will repeat for each word in new_words
        # “i” often refers to an integer counter rather 
        # than a string!
        if i == word:
           # if you want to see what is going on, you can
           # insert a print() line here:
           print("i = {}".format(i))
                   
           i = text.replace(i, "*" * len(i))
           # this will change “i” to be the whole string with
           # all instances of word changed to *** 
           # even if they are embedded inside other words
           # eg "a man and axe".replace("a","*")
        return "".join(i)
        # two problems: this will return straightaway, after
        # processing the first item in the loop. It should line
        # up with the “for” command!
        # The other problem is that it is not going to return 
        # what you think it will: “i” is a string value, not an
        # array, so joining it is not going to do much.
        # Also, changing the value of “i” does not feed back into
        # the new_word array, which is what I suspect you did 
        # mean to do the join on. 
    
print censor("hey hey hey", "hey")

Try this for a plan:

  1. split the text into an array of words (you did this)
  2. make a new empty array to hold your result
  3. for each word in the words array
  4. if the word matches, append a row of stars to the
    empty array
  5. if it doesn't, append the word as it is
  6. when the loop finishes, join up the new array into a
    string
  7. return the new string

Hope that makes some sense!!

Tim F


#4

def censor(text, word):
    ## lower case words in text
    text = text.lower()
    result = ""
    new_word = text.split(" ")
    ## iteration through words in text
    for i in new_word:
        if i == word:
            new_word.append("*" * len(i))
    ###    else:
    ###        new_word.append(i)
        result = " ".join(i)
    return result
    
print censor("hey hey hey", "hey")

MSG:Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "* * *" when it should return "*** *** ***".

Thank you for your help, I feel like I'm close but I don't know why its not appending the correct number of asterisks


#5

def censor(text, word):
    ## lower case words in text
    text = text.lower()
    result = []
    new_word = text.split(" ")
    ## iteration through words in text
    for i in new_word:
        if i == word:
            new_word.append("*" * len(i))
    ###    else:
    ###        new_word.append(i)
        result = " ".join(i)
    return result
    
print censor("hey hey hey", "hey")

changed result variable to empty array instead of string


#6

why did you comment out the else: new_word.append(i)?

if you uncomment else, we get a list containing our censored sentence. Then after the loop we need to .join() our result list and return it


#7

Hi there again.

You still need to be looking carefully at which variable you are using for what purpose:

  • i is a temporary holder for each word as you go through the loop

  • new_word is the array you use for holding the words as passed from the main program; you don’t need to change this once you created it; and appending to it is not what you mean to do.

  • result is where you should be collecting the words that you have censored or passed. It is an array, so this is where you should append the words one at a time.

    Also, don’t forget to append the okay words — you have commented out that part for some reason.

The return statement is now in the right place, but the result = " ".join is still part of the for loop. In Python you have to pay close attention to how the lines line up vertically!

To help with debugging, put something like this in the loop so that you see what is going on:

print ("i =", i, "; new_word = ", new_word, "; result = ", result)

but remember to take them out again afterwards once it’s working.

Getting closer I think!!

Tim F


#8

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