Thank you, again, Glenn. I knew we could turn to you for invaluable insight, and as usual you did not disappoint. Wonderful illustration!
This makes no sense.
def censor(text, word): temp_text = text.split() censored_text =  for i in temp_text: if i == word: censored_text.append("*" * len(word)) else: censored_text.append(i) censored_text = " ".join(censored_text) print censored_text
Gives me this error message:
Your function fails on censor(“hey hey hey”,”hey”). It returns “None” when it should return “* * ***”.
What? Why would the first two “hey” only be one star??
If I add a return to the end of my code, it’s all good. Something is broken with the console.
it shouldn’t, seems to be issue with the error message.
After def censor(text, word):, why is it text.split() instead of censor.split()?
Thanks so much!
text contains the string you want to censor. Censor is the function, which has two parameters:
text => the text you want to censor,
word => the word you want to censor from the text
Hi I got one doubt about this exercise, that I did not understood correctly: who accounts for what word will be censored? like, does the user choose a sentence and then choose which word will be censored or what?
In other words: how’s the definition of choosing the censored word is going to be done?
It is up to the programmer how the inputs will be taken, and how they will be used, and what will be returned. If we wish our program to take a blockquote and a censor term, then we have to be sure that we give both of those, and in the correct order.
The program can only work with the data it is given and expected to perform correctly in all cases. What is it supposed to be doing?
- take a string object
- redact censored words/phrases
- return censored string
The user’s role will be at the input stage, either by running the program and giving it input from the keyboard, or by calling the function from the command line with arguments it can run with.
By the way,
By reading the forum, trying on my own and etc, I’ve come up with this solution:
words = text.split()
result = ‘’
asterisk_count = ‘*’ * len(word)
stars_count = 0
for i in words:
if i == word:
words[count] = asterisk_count
result = ‘’.join(words)
I understood how it works, however, it is taking too long to come up with an error or saying that it is right. What happened?
Is there something I did wrong?
We cannot see where the variable,
count, is defined.
def censor(text, word): word_size= len(word) censored = "" i = 0 while i < len(text): if text[i : i + word_size] == word: censored+= word_size * "*" i+= word_size else: censored += text[i] i+= 1 return censored
I see that everyone went with “list”
is it necessary?
My code looks short and simple, is there a problem I dont see? (it passes)
code is hidden here
def censor(text, word): if word in text: text = text.replace(word,"*"*len(word)) return text
Depends on your use case, your approach has a “flaw”. It would also partially censor words. For example:
which would be problematic in some cases. For example, this forum filters curse words. But it would be nice the we write can write assignment, and don’t get
Does this way better? or is it a little bit advanced?
def censors(text,word): while word in text: z = text.replace(word, len(word) * '*') return z print(censors('like houses like houses with a yard', 'houses'))
The while loop is redundant. replace will find all instances and replaces them all
Depending on what you are after, this might also gives problem. Lets we ahve:
censor("this is a test", "is")
using replace, the outcome would be:
th** ** a test
this gets partially censored. Which might not be desirable.