-
Notifications
You must be signed in to change notification settings - Fork 0
/
mad_lib.py
51 lines (48 loc) · 1.75 KB
/
mad_lib.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
import re
adj = re.compile(r'ADJECTIVE(.)?')
noun = re.compile(r'NOUN(.)?')
adv = re.compile(r'ADVERB(.)?')
verb = re.compile('VERB(.)?')
wildcard = re.compile(r'(.)?')
file_path = str(input('Type in absolute path to file: '))
new_file_path = file_path + '_new'
new_text = list()
with open(file_path) as file:
file_content = file.readlines()
file.close()
with open(new_file_path, 'w') as new_file:
for line in file_content:
content_split = line.split()
for y in content_split:
dot = False
for z in y:
if wildcard.search(z):
dot = True
character = z
if adj.search(y):
if dot:
new_text.append(str(input('Type in an adjective: ') + character))
else:
new_text.append(str(input('Type in an adjective: ')))
elif noun.search(y):
if dot:
new_text.append(str(input('Type in an noun: ') + character))
else:
new_text.append(str(input('Type in an noun: ')))
elif adv.search(y):
if dot:
new_text.append(str(input('Type in an adverb: ') + character))
else:
new_text.append(str(input('Type in an adverb: ')))
elif verb.search(y):
if dot:
new_text.append(str(input('Type in an verb: ') + character))
else:
new_text.append(str(input('Type in an verb: ')))
elif y:
new_text.append(y)
string = ' '.join(new_text)
print(string)
new_file.write(string)
new_file.write('\n')
new_text.clear()