Hey guys,
I cannot be quite sure why this code is not working. With an input of (hello world, world), it returns "hello world".
Any advice is appreciated, Thanks :slight_smile:

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

print censor(raw_input("String"), raw_input("Word"))


The indentation is way out of whack. Are you sure you understand the meaning and purpose of indentation in Python? Review the lessons on syntax if you are unsure.

This is having no effect. See if you can find out why not.


Oh sorry, I didn't realize the code did not copy correctly, I will fix it now.

I'm not exactly sure why the x = "*" * len(word) is not working, It should be capable of storing the new string in the list of words... but I will try this instead, thanks.


x is the iterator variable, only. If we change it, nothing in the object changes, so it goes unregistered.

However, there are other ways, one of which may not yet have been covered... .index(). If we find the index associated with the item (x in this case) then we can modify the object directly. This is something I'll leave with you to discover, and in the meantime, see if you can do this with what we know at this point, perhaps using range()?


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