10. censor


#1
  1. What's the problem?
    def censor(text, word):
    for x in text:
    for a in word:
    a = '*'
    return text

  2. Can't we access the data under word directly from text?
    for word in text?
    word = '*' * len (word)

  3. Why is .split() so significant here/generally?


Censor lesson #10
#2

Deadlock! Please help.

def censor(text, word):
text = text.split()
for x in str(text):
for a in str(word):
a = '*'
text = " ".join(text)
return text


#3

This function is useful for converting a string of words (space separated sentence or phrase) to a list of words with no white space.

"this is a sentence of words"

becomes,

['this', 'is', 'a', 'sentence', 'of', 'words']

which permits us to iterate over each word, individually. In this program we are looking for one word to censor, so need to compare each word in the list to the censor word. If it matches, then we replace the word with an asterisk for each letter.

if x == word:

We only need one loop, not two. Take one more kick at this and we can review possible approaches.


#4

def censor(text, word):
text = text.split()
for x in text:
if x == word:
a = []
a.append(x)
a = '*' * len (word)
text = " ".join(text)
return text

"""
keyis your for loop iterator, its get assigned the values from the list. Any changes made to it will not persist.

create a list/string and append the words to this string/list, censor when needed, you decide if a list or a string is better
"""

I got stuck here now

How to bring the censor back into the text?

What are the other possible approaches?

Thank you for a succinct response.


#5

Hello?
1. How to replace the matched word in the text?

def censor(text, word):
text = text.split()
for x in text:
if x == word:
a = []
a.append(x)
a = '*' * len (word)
text = " ".join(text)
return text

Idk, please help.


#7

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