I'm not sure why the loop misses the first hey in the input string. Here is the error it returns.
Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "hey *** *** " when it should return "*** *** ***".

What am I missing?

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


For one thing, your split function puts quotation marks around every word, and your code doesn't account for that with a .join approach. Also, I doubt this will fix it, but try replace instead of remove + append.


I updated my code with the " "join.(a_list), but the replace function didn't work for me so I left that out. Any other suggestions?


Alright, I'm currently messing around with your code, but I have a few things you have to fix. First, " "join.(a_list) is invalid syntax. It should be " ".join(a_list). Also, after all the asterisks you have to include a space, but you want to be able to remove that space if it is at the end. (Thus, you need an extra append statement.)


Whenever you append something it goes to the back, so that would sort your asterisks to the very end. Consider using two lists: a_list for the broken text, and new_list for what you want to return. Then you can go through the elements in a_list and if they are equal to the word, you can append asterisks into new_list, along with a space. Otherwise, you can just put the word in new_list. Please note: I tried this approach and faced a couple difficulties which I had to debug. One of these difficulties was that this ended up adding an extra space at the end. Think of how to solve that.


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