I am trying to bleep the message that the user gives as input , as if the input is "what the heck" where "heck" is present in list of banned words in a file called "banned.txt" so that the output becomes "what the ****" .
i am a newbie to python and till now I've made two list out of the passed input as well as the list in which banned words are present , i am having trouble comparing the words in these two list , can someone explain me how to solve this problem.
from cs50 import get_string
import sys
def main():
if len(sys.argv) != 2:
print("Usage: python bleep.py dictionary")
exit(1)
print("What message would you like to censor?")
msg=get_string()
infile=open(sys.argv[1],'r')
bannedwords=[infile.read().split("\n")]
userwords=[msg.split(" ")]
for uword in userwords:
for bword in bannedwords:
if(uword==bword)
#do something.....but its not comparing words
if __name__ == "__main__":
main()
Input: what the heck
Expected-Output: what the ****
Instead of splitting your msg int a list, search it for the banned word.
for word in bannedwords:
if word in msg:
new_word = "*" * len(word)
new_msg = msg.replace(word, new_word)
if you would like to test it directly use this:
bannedwords = ["banned", "other"]
msg = "the next word is banned"
new_msg = ""
for word in bannedwords:
if word in msg:
new_word = "*" * len(word)
new_msg = msg.replace(word, new_word)
print(new_msg)
Your problem is in the lines:
bannedwords=[infile.read().split("\n")]
userwords=[msg.split(" ")]
The split command already puts the words into an array, so you're wrapping them in two arrays, getting:
[['heck', '']]
[['what', 'the', 'heck']]
as the list of banned words, and the list of words from the message respectively.
Remove the []
from the definitions and use:
bannedwords=infile.read().split("\n")
userwords=msg.split(" ")
and your code should work as you wanted. You could also just check if each word is in the list of banned words, for example:
bannedwords=infile.read().split("\n")
userwords=msg.split(" ")
for uword in userwords:
# An easier way to check if the word is banned.
if(uword in bannedwords):
# If it's banned, print one * for each letter in the word.
print("*" * len(uword))
else:
print(uword)
with input of 'what the heck' gives an output of:
what
the
****
The following solution may work for you. This also removes punctuations from the end of the msg
string while replacing bannedwords
in msg
with '*':
from string import punctuation
bannedwords = ["heck", "darn"]
msg = input("What message would you like to censor?: ")
for word in msg.rstrip(punctuation).split(' '):
if word in bannedwords:
new_word = '*' * len(word)
msg = msg.replace(word, new_word)
print(msg)
#Output:
What message would you like to censor?: What the heck?
What the ****?
#Another attempt:
What message would you like to censor?: darn it!
**** it!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.