Censor


#1

Here's my code, I'm programming for 2 weeks in python now

def asterisks(num):
.. res = ""
.. while num>0:
....... res += '*'
.. return res

def censor(text, word):Preformatted text
.. final_text = ""
.. number_of_asterisks = len(word)
.. text_letters= []
.. for i in text:
........ text_letters.append(i)
.. word_list= []
.. between= ""
.. for i in text:
........ if i == " ":
............... for let in text_letters:
...................... while let != i:
.............................. between += let
.............................. text_letters.remove(let)
..................... text_letters.remove(i)
............... word_list.append(between, " ")
............... between=""
.. for i in word_list:
....... if i == word:
............. word_list[i] = asterisks(number_of_asterisks)
.. for i in word_list:
........ final_text += i
........ final_text = final_text[:-1]
.. return final_text

Edit: Sorry, it isn't formatting here...

It tells me following: Oops, try again. Your code looks a bit off--it threw a "list.remove(x): x not in list" error.

What shall i do ?


#2

Your error message is describing a problem, you'll need to consider why that problem shouldn't have occurred and which part of your code is responsible for that.

Since the particular problem being described is removing something that isn't there, what should have happened in your code is to only remove things that are known to be there. Or if it's not known, then you should probably be doing something else instead (usually it's a good idea to refer to what you'd do manually, your program should probably be carrying out the same actions)


#3

tested it out. for example
letter_list= ["h","a","v",.....]
it gets letter_list.remove("h")
and i get the error, i also tested it in an abstract form on repl.it but it worked there perfectly fine
my test:

scor = "abc"
score = []
for i in scor:
score.append(i)

print score

score.append("BLABLABLA")

print score

score.remove("BLABLABLA")

print score

score.remove("a")

print score

i got:
['a', 'b', 'c']
['a', 'b', 'c', 'BLABLABLA']
['a', 'b', 'c']
['b', 'c']


#4

After you define the function, what is next?

You have to figure out if the word is in the text. Well what happens if it is?
You have to replace the word in the text with "*" multiplied by the length of the word.
Then you return the text.

Four lines of code is all that is needed. Example:

def censor(text, word):
    if word in text:
        text = text.replace(word, "*" * len(word))
    return text

I just read the hint for this exercise, and while it says that splicing may help you, it is not an absolute requirement.


#5

Sure, if you add something to the list, then it's going to be there when you remove it.
That's not the described scenario though. The scenario was that the value you tried to remove wasn't there. Perhaps it was never there in the first place, or perhaps you had removed it an an earlier point in time. You'll need to have some reasoning about why you would be able to remove something before you do so.
You can add similar prints in your censor code to create a log of what you are adding/removing from your list.

By the way, are you sure list.remove behaves like the action you want to carry out? There's nothing saying that there won't be repeated letters in the text and you might not want to remove whichever occurrence happens to come first.

You've also got a loop where nothing that the condition tests changes as the loop runs. So the condition is either going to be false and there will be no iterations, or it's going to be true and it'll iterate forever.

My suggestion is to grab pen and paper and run your algorithm manually to convince yourself that it is correct before you start writing any of it in code. Or just do it manually and study how you yourself do it, and break that down into operations that can be carried out with lists/strings. Once you have a set of instructions that you think is correct, implement one small part at a time and use print statements to check that they are being carried out correctly in the code. Also consider whether input can be constructed to break your algorithm or argue for why it's always correct.


#6

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