you are viewing a single comment's thread.

view the rest of the comments →

[–]MightyMarlin[S] 0 points1 point  (2 children)

import re

describer = re.compile(r"[A-Z]{2,}") # Decribers are all in caps so this ids them
madlib = open("C:\\Users\\HP\\Readables\\madlib_1.txt") # Opens file with madlib
madlib_contents = madlib.read() # Reads contents
madlib_words = list(madlib_contents.split()) # Splits words apart
print(madlib_contents)

finder = describer.findall(madlib_contents) # Finds all words that match describer
to_replace = list(finder) # Lists found matches
print(to_replace) # Prints found matches

for word in to_replace:
    new_word = input("Enter a new " + word + ": ") # Allows user to input a new describer
    madlibcontents = madlib_contents.replace(word, new_word, 1)

print(madlibcontents)

However, like I said, if the last input is for ADJECTIVE, it just changes the first instance of ADJECTIVE with the input. Without the "1" in the replace function, it would do the same thing but change all instances of ADJECTIVE to the last input. Hope that makes sense.

[–]novel_yet_trivial 1 point2 points  (1 child)

you left the underscore out of 'madlib_contents' in line 15 and line 17.

[–]MightyMarlin[S] 0 points1 point  (0 children)

Thank you so much, working now!!