SHARE
TWEET

Untitled

a guest Jun 24th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from cs50 import get_string
  2. from sys import argv, exit
  3.  
  4.  
  5. def main():
  6.     # ensure correct usage
  7.     if len(argv) != 2:
  8.         print("Usage: python bleep.py dictionary")
  9.         exit(1)
  10.  
  11.     # open dictionary of bad words
  12.     name = argv[1]
  13.     text = open(name, "r", encoding="latin_1")
  14.     if not text:
  15.         print("Could not open {}.".format(name))
  16.         unload()
  17.         exit(1)
  18.  
  19.     # store banned words in list
  20.     bannedList = []
  21.     for ban in text:
  22.         ban = ban.replace('n', '')
  23.         bannedList.append(ban)
  24.  
  25.     # get user input to censor and split into words
  26.     toCensor = input("What message would you like to censor? ")
  27.     toCensor = toCensor.split()
  28.  
  29.     # for censored phrase
  30.     final = []
  31.  
  32.     # check if word needs to be censored
  33.     for word in toCensor:
  34.         if word.lower() in bannedList:
  35.             censored = word.replace(word, "*"*len(word))
  36.             final.append(censored)
  37.         else:
  38.             final.append(word)
  39.  
  40.     print(' '.join(final))
  41.  
  42.  
  43. if __name__ == "__main__":
  44.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top