Advertisement
RockField64

Madlibs-Generator

Jun 30th, 2024
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | Source Code | 0 0
  1. # This opens the file story.txt.
  2. with open("story.txt", "r") as s:
  3.     story = s.read()
  4.  
  5. # Find words in parentheses
  6. words = set()
  7. beginning_of_word = -1
  8.  
  9. start_target = "("
  10. end_target = ")"
  11.  
  12. for i, char in enumerate(story):
  13.     if char == start_target:
  14.         beginning_of_word = i
  15.  
  16.     if char == end_target and beginning_of_word != -1:
  17.         word = story[beginning_of_word: i + 1]
  18.         words.add(word)
  19.         beginning_of_word = -1
  20.  
  21. # Get replacements
  22. answers = {}
  23.  
  24. for word in words:
  25.     answer = input("Enter a word " + word + ": ")
  26.     answers[word] = answer
  27.  
  28. # Replacing words in the story
  29. for word in words:
  30.     story = story.replace(word, answers[word])
  31.  
  32. print(story)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement