hello, i need help with the below code-

def sensor(text):
text = ‘’
for word in text:
if word == hack:
word = ‘****’
word = word
text += word
return text
print sensor(“What the hack”)

Where is the problem in this code? When i run this code, console show nothing


For starters. Why reset the value that has been handed in?

Be sure to give your function the name that the lesson checker expects: censor.


Hey, is anything in that code is right?
In “get a hint” they suggesting to use a string.split(), is it mean that python “can’t” read string by words, it only can read string as a list of letters? In that case, I should to make Python to choose letters that standing in special order to match with word that hide in second parametre of function. It seems to be too complicated for such task. Where do I wrong?


Python only reads strings as characters (bytes). To get it to expand that to words we need to separate them from the string into a list. That is what .split() returns, a list of words.

text = "This is a string of characters to the computer."

words = text.split()

print (words)    

# ['This', 'is', 'a', 'string', 'of', 'characters', 'to', 'the', 'computer.']

Now it is a list of strings.


Aaaah! I’ve been should to read more about .split function before doing the task. Thank you very much!!!:grinning:


There are a number of errors, but without the lesson in front of us it is difficult to point them out with any authority.

hack should be written as a string, "hack". But that brings up the question, should this not be a parameter so the function can be multi-purpose (usable with any word or phrase)?

def censor(text, term):

if word == term:


And in spite of all ouer talks my code doesn’t work :sweat_smile:

def censor(text,word):
  text = text.lower()
  word = word.lower()
  for term in words_lst:
    if term == word:
  new_text=" ".join(words_lst)
  return new_text

As I findout the function works before “for”, it does “split” the string to a list, and then it does “join” the list to a string, but it doesn’t do the “for”, and I can’t find a mistake :sweat:


why convert to lowercase? I don’t think its a good idea

term (the loop iterator) will contain read-only values from the list (word_lst), no changes are made to the list. Any changes made to term won’t persist given the loop will just assign the next value to the loop iterator


It’s a condition of the task

I’ve put “return term”, is it right? I don’t know another way to save the changes.


assume your strings won’t contain any punctuation or uppercase letters, so the letters will be all lowercase, so converting to lowercase isn’t necessary.

Why would you do that? By default a function returns None at the end of the function, if we want to return something else at the end of the function, we can use the return keyword. But given returning is the last thing a function will do, a function will end when a return keyword is encountered

there are two common ways to solve this problem:

  • create a new list and append to list, apply censoring where needed (if/else)
  • update words in the list which needs censoring (using indexes)


Ooook, thank you!:slightly_smiling_face:
I will try to do both ways


I’ve done the first way, thats what I got :

the function works, but emulator somehow doesn’t like it


Ooops! Sorry! I forgot to remove converting to a lowercase, now it works!)))))

Is it a problem just for emulator, the code doesn’t wrong with convert to a lowercase, or it do make some logical mistake?


This is the second way, I found this method in some forum:
def censor(text,word):

for i,item in enumerate(words_lst):
if item==word:

new_text=" ".join(words_lst)
return new_text

Is it that what you meaned?


If all you do is censoring, you don’t want to change everything to lowercase. This contradicts the instructions, but i think this issue is known

Its the best way, yes. range() would also have been possible, that is a function you should know, but enumerate is better for this use case



i would do once outside the loop, no need to construct this string over and over again each iteration of the loop


:thinking: you confounded me : how to use range here?:weary:


I mean, how to conform numerals and strings?


we can use the index (numeral) to get the value from the list and use it in the comparison:

if the_list[index] == word:


Oh gosh, it’s so easy! I feel stupid))) Thank you, now it is all clear for me:blush:


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