From 75a42ec54dbf721caa659ddf02c1f46fc2cb4bef Mon Sep 17 00:00:00 2001 From: mlot Date: Fri, 6 Jun 2025 13:40:57 -0400 Subject: initial commit for archiving --- Chap8PracMadLibs.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Chap8PracMadLibs.py (limited to 'Chap8PracMadLibs.py') diff --git a/Chap8PracMadLibs.py b/Chap8PracMadLibs.py new file mode 100644 index 0000000..785b55f --- /dev/null +++ b/Chap8PracMadLibs.py @@ -0,0 +1,41 @@ +#! \usr\bin\env python3 + +# Chapter 8 Practice Mad Libs + +# Replaces all ADJECTIVE, NOUN, ADVERB, VERB keywords with user input in text. + +import re + + +with open('madlibs.txt') as madLibFile: + madLibContents = madLibFile.read() +madLibWords = list(madLibContents.split()) + +adjectiveRegex = re.compile(r'ADJECTIVE*') +nounRegex = re.compile(r'NOUN*') +adverbRegex = re.compile(r'ADVERB*') +verbRegex = re.compile(r'VERB*') + +for i in range(len(madLibWords)): + if adjectiveRegex.match(madLibWords[i]): + print('Enter an adjective', end=': ') + sub = input() + elif nounRegex.match(madLibWords[i]): + print('Enter a noun', end=': ') + sub = input() + elif adverbRegex.match(madLibWords[i]): + print('Enter an adverb', end=': ') + sub = input() + elif verbRegex.match(madLibWords[i]): + print('Enter a verb', end=': ') + sub = input() + else: + continue + madLibWords.remove(madLibWords[i]) + madLibWords.insert(i, sub) + +joinWordsList = ' ' +newString = joinWordsList.join(madLibWords) +with open('madlibsNew.txt', 'w') as newMadLibFile: + newMadLibFile.write(newString) +print(newString) -- cgit