Python-censor


#1

hello, i need help with the below code-

def sensor(text):
text=text.lower()
text = ‘’
for word in text:
if word == hack:
word = ‘****’
else:
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


#2

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.


#3

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?


#4

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.


#5

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


#6

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:

#7

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

def censor(text,word):
 
  text = text.lower()
  word = word.lower()
  words_lst=text.split()
  for term in words_lst:
    if term == word:
        term="*"*len(word)
    else:
      term=term
  
  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:


#8

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


#9

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.


#10

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)

#11

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


#12

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

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


#13

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?


#14

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

words_lst=text.split()
for i,item in enumerate(words_lst):
if item==word:
words_lst[i]="*"*len(word)

new_text=" ".join(words_lst)
return new_text

Is it that what you meaned?


#15

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

this:

"*"*len(word)

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


#16

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


#17

I mean, how to conform numerals and strings?


#18

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

if the_list[index] == word:

#19

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


#20

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