-
Notifications
You must be signed in to change notification settings - Fork 10
/
(week 6) - bleep.py
66 lines (49 loc) · 1.6 KB
/
(week 6) - bleep.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# It's run with one command line argument (dictionary file of cuss words)
# Asks for a string
# Spits back the string with the words in dictionary censored with *
#
#
from cs50 import get_string
from sys import argv
import sys
def main():
# accept one command line argument
while True:
if (len(argv) == 2):
break
print("Usage: python bleep.py dictionary")
sys.exit(1)
break
inputFile = argv[1]
# open dictionary
infile = open(inputFile, 'r')
words = []
# Read all words and append to a list
for line in infile:
word = infile.readline()
words.append(line[:-1])
words.append(word[:-1])
# Working on the user inputed string to censor
sentence = get_string("What message would you like to censor?\n")
sentence.lower()
censorList = sentence.split()
censorListCopy = sentence.lower().split()
# iterating over banned words
for i in range(len(words)):
# iterating over words in string
for j in range(len(censorListCopy)):
if words[i] == censorListCopy[j]:
# making a censored copy of the word
copy = censorList[j]
copyLength = len(copy)
copy = ""
print(f"copy: {copyLength}")
# replace word with *
for i in range(copyLength):
copy = copy + '*'
censorList[j] = copy
# print back sentence with censored words
my_lst_str = " ".join(map(str, censorList))
print(my_lst_str)
if __name__ == "__main__":
main()