-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmadlibgenratorgpt.py
More file actions
45 lines (34 loc) · 1.13 KB
/
madlibgenratorgpt.py
File metadata and controls
45 lines (34 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Open the file and read its content
with open('story.txt', 'r') as f:
story = f.read()
# Initialize variables
#store unique placeholders (e.g., <hero>). Using a set ensures no duplicate entries.
words = set()
#uses -1 to ensure no place holder is being assigned the moment
start_of_word = -1
target_start = '<'
target_end = '>'
# Extract words between the target_start and target_end
#char is the letter you are looking for while i is the position of it
for i, char in enumerate(story):
if char == target_start:
start_of_word = i
if char == target_end and start_of_word != -1:
word = story[start_of_word:i+1]
words.add(word)
start_of_word = -1
# Create a dictionary to store replacements
answers = {}
# Prompt user for replacements
for word in words:
answer = input(f"Enter a word for {word}: ")
answers[word] = answer
# Replace placeholders in the story
for word in words:
story = story.replace(word, answers[word])
# Output the updated story
print("\nUpdated Story:")
print(story)
# Optionally, save the updated story back to the file
with open('story.txt', 'w') as f:
f.write(story)