Censor


#1

When I write:

x = " ".split(“this is a test”)

My code returns an empty list.
When I rewrite it like so:

x = “this is a test”.split(" ")

I get the result you are describing in your answer. Why is that?


How can I use split( ) to make censor?
#2

the general syntax of split is:

"string to split".split(separator)

where the separator is what you want to use for separation:

print "to,show,how,this,works".split(",")
print "to,show,how,this,works".split()

see the docs:

https://docs.python.org/2/library/string.html#string.split


#3

Thanks a lot for this clarification. I’m still kind of stuck. This is what I have so far. But I always get an indentation error for the second for-loop. Is this even the right approach, or am I completely on the wrong track here?

def censor(text,word):
  text = text.split(" ")
  for x in range(0,len(text)):
    if  text[x] == word:
    	word_censored = ""
    	  for y in range(0,len(word)):
        word_censored += "*"
      text[x] = word_censored
 	return text.join(" ")


#4

the approach is most certainly right. Apart from the indention issues and calling .join() incorrectly, the logic of your program is working.

i will leave you to trying to fix the indent issue, let me know if you need more help. One more tip, codecademy’s editor is not the nicest one to work in when it comes to fixing indent issues. You might want to use your own preferred editor.


#5

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