Hello all.

So I am trying to write a program that will read in a text file, then it will scan the file for prompts, such as %, %[noise], etc. Then from what prompts are within that file, the program will ask the user what word they would like to replace the prompt with.

Example stories:

%[adjective] MacDonald had a %[business], E-I-E-I-O.
And on that %[business] he had a %[animal], E-I-E-I-O.
With a %[noise] %[noise] here, and a %[noise] %[noise] there,
here a %[noise], there a %[noise], everywhere a %[noise] %[noise],
%[adjective] MacDonald had a %[business], E-I-E-I-O.

So it would find the %[adjective], %[business], %[animal], %[noise]. Then ask, what adjective, business, animal and noise the user would like to use.


myFile=raw_input("What is the file name?: ")
myFile=open(myFile)
myFile=myFile.read()

myString1 = raw_input("Type an adjective: ")
myString2 = raw_input("Type a business: ")
myString3 = raw_input("Type a animal: ")
myString4 = raw_input("Type a noise: ")

Punct=["[","]","%"]
s = myFile
np=s
for p in Punct:
    np=np.replace(p,"")
    
np = np.replace("adjective", myString1)
np = np.replace("business", myString2)
np = np.replace("animal", myString3)
np = np.replace("noise", myString4)

print np

That is my code for this story, but I don't know how to write the program to scan for the tags and prompt the user based on the tags within the text file. It needs to work with other stories like the following.

%[girls_name] had a little %[animal],
little %[animal], little %[animal],
%[girls_name] had a little %[animal]
its fleece was % as snow.

It followed her to %[place] one day,
%[place] one day, %[place] one day,
it made the children %[verb] and play
to see a %[animal] at %[place].

So ask for girls_name, animal, color, place and verb.

Any tips or points into the right direction would help a lot. I am sorta new to python, I feel stupid. But would love the feedback, thanks!

You can use a regular expression (the re module) to scan the text

import re

text = """
%[girls_name] had a little %[animal],
little %[animal], little %[animal],
%[girls_name] had a little %[animal]
its fleece was %[color] as snow.

It followed her to %[place] one day,
%[place] one day, %[place] one day,
it made the children %[verb] and play
to see a %[animal] at %[place].
"""

regex = re.compile(r"\%\[(\w+)\]")

#============================================================================
# this section prints 
#['girls_name', 'animal', 'animal', 'animal', 'girls_name', 'animal', 'color', 'place', 'place', 'place', 'verb', 'animal', 'place']

print regex.findall(text) 

#============================================================================
# this section prints
# %[girls_name]
# %[animal]
# %[animal]
# %[animal]
# %[girls_name]
# %[animal]
# %[color]
# %[place]
# %[place]
# %[place]
# %[verb]
# %[animal]
# %[place]

for match in regex.finditer(text):
    print match.group(0)

#============================================================================
# this section prints
# GIRLS_NAME had a little ANIMAL,
# little ANIMAL, little ANIMAL,
# GIRLS_NAME had a little ANIMAL
# its fleece was COLOR as snow.
#
# It followed her to PLACE one day,
# PLACE one day, PLACE one day,
# it made the children VERB and play
# to see a ANIMAL at PLACE.

def transform(match):
    return match.group(1).upper()

print regex.sub(transform, text)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.