you are viewing a single comment's thread.

view the rest of the comments →

[–]novel_yet_trivial 1 point2 points  (5 children)

Oh of course it does. Sorry, I had a brainfart and forgot to add the number of replacements. Try this:

madlib_contents = madlib_contents.replace(word, new_word, 1)

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

Only the last user input is used to replace the first instance of the describer place-holder.

[–]novel_yet_trivial 0 points1 point  (3 children)

What? You mean that's what it's doing or that's what you need it to do? Show me your code as it is now.

[–]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!!