from automate boring stuff python book: "create mad libs program reads in text files , lets user add own text anywhere word adjective, noun, adverb, or verb appears in text file. example, text file may this:
the adjective panda walked noun , verb. nearby noun unaffected these events.
the program find these occurrences , prompt user replace them."
i'm done, can't seem figure out how .join last list on file. i've looked online , tried methods. instead of getting string out of join, whitespace in ' '.join(mod4) separating letters within each list string value. [ ' n ' , ' o n e ' , ' t w o ' , ' n d ' , ' t h r e e ' , ' f o u r ' ] else job.
#! python3 import re madtext = open('madtext.txt', 'w') text = 'an adjective, noun, adverb , verb.' madtext.write(text) madtext.close() content = re.split('\w+', text) in content: if == "adjective": replaceregex = re.compile(r'(adjective)') print('enter adjective:') adjective = input() output = replaceregex.sub(adjective, str(content)) elif == "noun": replaceregex = re.compile(r'(noun)') print('enter noun:') noun = input() output = replaceregex.sub(noun, str(output)) elif == "adverb": replaceregex = re.compile(r'(adverb)') print('enter adverb:') adverb = input() output = replaceregex.sub(adverb, str(output)) elif == "verb": replaceregex = re.compile(r'(verb)') print('enter verb:') verb = input() output = replaceregex.sub(verb, str(output)) content = re.split('\w+', output) #content = list(output.split(' ')) content = ' '.join(content) print(content) madlibs = open('madtext2.txt', 'w') madlibs.write(content) madlibs.close()
you have basic assumption preventing completing this. assignment of mod4
based on previous assignments , order.
instead should doing initializing output
variable []
, appending words loop through content
. add adlib words along real words.
once have built output list, then use join
turn output
string.
also, using regex
overkill. let's assume you've made output = []
before loop.
if == 'noun': print('enter noun:') noun = input() # raw_input() on python 2 output += noun [...]
now hit each adlib token replace inputted text , build output list.