58 lines
1.7 KiB
Plaintext
58 lines
1.7 KiB
Plaintext
def madlib(template)
|
|
// loop through and find/remove all of the replacements
|
|
replacements = {}
|
|
ind = 0
|
|
while ind < len(template)
|
|
// check if we have found a replacement to perform
|
|
if template[ind] = "<"
|
|
// get the name of the replacement and add it to the list
|
|
// if we haven't already encountered it
|
|
ind += 1
|
|
replace_name = ""
|
|
while template[ind] != ">"
|
|
replace_name += template[ind]
|
|
ind += 1
|
|
end while
|
|
|
|
if not replace_name in replacements
|
|
replacements.append(replace_name)
|
|
end if
|
|
ind += 1
|
|
else
|
|
ind += 1
|
|
end if
|
|
end while
|
|
|
|
// prompt the user for replacement values
|
|
replacement_values = {}
|
|
for phrase in replacements
|
|
replacement_values.append(input("enter " + phrase + ": "))
|
|
end for
|
|
println
|
|
|
|
// make replacements and output the story
|
|
ind = 0
|
|
while ind < len(template)
|
|
// check if we have found a replacement to perform
|
|
if template[ind] = "<"
|
|
// get the name of the replacement
|
|
ind += 1
|
|
replace_name = ""
|
|
while template[ind] != ">"
|
|
replace_name += template[ind]
|
|
ind += 1
|
|
end while
|
|
|
|
// output the replacement
|
|
print replacement_values[replacements[replace_name]]
|
|
ind += 1
|
|
else
|
|
print template[ind]
|
|
ind += 1
|
|
end if
|
|
end while
|
|
end madlib
|
|
|
|
madlib("<name> went for a walk in the park. <he or she> " + \
|
|
"found a <noun>. <name> decided to take it home.")
|