Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit fd64c07

Browse files
committed
lets the user add their own text anywhere in the text file
1 parent d146699 commit fd64c07

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

projects/mad-libs/input.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The ADJECTIVE panda walked to the NOUN and then VERB.
2+
A nearby NOUN was unaffected by these events.

projects/mad-libs/mad-libs.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
import re
3+
4+
def madLibs(input_file, output_file):
5+
"""lets the user add their own text anywhere the word ADJECTIVE, NOUN, ADVERB, or VERB appears in the text file
6+
Args:
7+
filename (str): name of file to parse
8+
Returns:
9+
None
10+
"""
11+
regex = re.compile(r'(NOUN|ADJECTIVE|ADVERB|VERB)')
12+
13+
with open(input_file, 'r') as in_file, open(output_file, 'w') as out_file:
14+
15+
content = in_file.read()
16+
17+
matches = regex.findall(content)
18+
19+
for found in matches:
20+
sub = input('Enter a ' + found + ': ')
21+
content = content.replace(found, sub, 1)
22+
23+
out_file.write(content)
24+
print(content)
25+
26+
if __name__ == "__main__":
27+
madLibs('input.txt', 'output.txt')

projects/mad-libs/output.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The a panda walked to the b and then c.
2+
A nearby d was unaffected by these events.

0 commit comments

Comments
 (0)