Files
AutomateTheBoringStuffWithP…/practice_projects/mad_libs.py
2017-06-19 18:59:52 +08:00

40 lines
1.2 KiB
Python

#! python3
'''
mad_libs.py - reads in text files and lets the user add their own text anywhere
the word ADJECTIVE, NOUN, ADVERB, or VERB appears in the text
file.
For example, a text file may look like this:
```
The ADJECTIVE panda walked to the NOUN and then VERB.
A nearby NOUN was unaffected by these events.
```
The program would find these occurrences and prompt the user to
replace them.
Enter an adjective:
silly
Enter a noun:
chandelier
Enter a verb:
screamed
Enter a noun:
pickup truck
The following text file would then be created:
```
The silly panda walked to the chandelier and then screamed.
A nearby pickup truck was unaffected by these events.
```
'''
import re
file_name = input("Enter the filename:")
file_read = open(file_name)
content = file_read.read()
for to_replace in re.compile(r'ADJECTIVE|NOUN|ADVERB|VERB').findall(content):
content.replace(to_replace)