Censor


#1

I have this code, but it did not work:

def censor(text, word):
 new_string_array = []
 text = text.split()
 m = "*" * len(word)
 for i in text:
      if i == word:
          new_string_array.append(m)
      return str(new_string_array) 
 else:
    new_string_array.append(i)

I apologize for butting in:sweat:


Censor
#2

how can your code have no indention at all?


#3

I did ident it, it is just that when I copy and paste, it comes out like this>


#4

but i applied markup, then indention should show?


#5

I tried this new code:

def censor(text, word):
    new_string_array = []
    text = text.split()
    m = "*" * len(word)
    for i in text:
         if i == word:
             print new_string_array.append(m)
         else:
              new_string_array.append(i)
    return str(new_string_array)

It still didn't work!

P.S. I have edited the first one above.


#6

your latest solution, you are pretty close. But using str() will just cast the entire list into a string:

"['***', '***', '***']"

you need to convert the list into a string, for this we can use .join()


#7

Everything in your code looks to be fine. But, the last return command is returning a list and not a string : it seems , you can't use 'str' to convert a list to string . Just convert your list to string by using :
var = " ".join(new_string_array)
return var
Hope , this helps !
:slight_smile:


#8

When you are checking if i == word, you are comparing a charcater to a full word, so it never turns true


#9

2 posts were split to a new topic: Censor


#10

3 posts were split to a new topic: Censor


#11

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