I have a banned_word list banned=["things", "show","nature","strange image"]
, ,and I need to read a forum message txt file and replace all the banned words in the forum file by same length asterisks.
This is the forum file
Forum ONE
Are things real?
What is nature?
Show me your image.
You have shown me a strange image, and they are strange prisoners.
The expected output is
Forum ONE
Are ****** real?
What is ******?
**** me your image.
You have shown me a *************, and they are strange prisoners.
My actual output is
Forum ONE
Are ****** real?
What is ******?
Show me your image.
You have ****n me a *************, and they are strange prisoners.
The banned word is case insensitive, so Show with capital S is counted as banned, but shown should not be counted as the banned word.
Below is my code
#banned_list
banned=["things","nature","strange image","show"]
#read message
with open("forum1","r")as f:
message=f.readlines()
#append modified message in a new list
new_forum=[]
i=0
while i<len(message):
j=0
while j<len(banned):
if message[i].__contains__(banned[j]):
message[i]=message[i].replace(banned[j],len(banned[j])*"*")
j+=1
else:
j+=1
new_forum.append(message[i])
i+=1
#write to a new_list
with open("new_forum1","w")as n:
i=0
while i<len(new_forum):
n.write(new_forum[i])
i+=1
Since this is a school homework, I am not allowed to use for and in. How should I modify my code?