Private Python2VBText As New TextBox ' ' madlib2.py ' Interactive display of a mad lib, which is provided as a Python format string, ' with all the cues being dictionary formats, in the form %(cue)s. ' ' In this version, the cues are extracted from the story automatically, ' and the user is prompted for the replacements. ' ' Original verison adapted from code of Kirby Urner ' Public Function getKeys(formatString As Object) ' formatString is a format string with embedded dictionary keys. ' Return a set containing all the keys from the format string. Dim keyList As List(Of Object) keyList = New List(Of Object) Dim end2 As Integer end2 = 0 Dim repetitions As Object repetitions = formatString.count("%(") Dim key As Integer Dim start As Object for i = 0 To repetitions - 1 start = formatString.find("%(", end2) + 2 end2 = formatString.find(")", start) key = Mid(formatString, start + 1, end2 - start ) keyList.add(key) ' may add duplicates Next Return keyList ' removes duplicates: no duplicates in a set End Function Public Sub addPick(cue As Object, dictionary As Object)' from madlib.py ' Prompt the user and add one cue to the dictionary. Dim prompt As String prompt = "Enter a specific example for " + (cue).toString + ": " dictionary(cue) = InputBox(prompt) End Sub Public Function getUserPicks(cues As Object)' from madlib.py ' Loop through the collection of cue keys and get user choices. ' Return the resulting dictionary. ' Dim userPicks As New Dictionary(Of Object, Object) for cue= 0 To cues.Length - 1 addPick(cues(cue), userPicks) Next Return userPicks End Function Public Sub tellStory(story As Object)' from madlib.py ' story is a format string with Python dictionary references embedded, ' in the form %(cue)s. Prompt the user for the mad lib substitutions ' and then print the resulting story with the substitutions. ' Dim cues As Object cues = getKeys(story) Dim userPicks As Object userPicks = getUserPicks(cues) Python2VBText.Text = Python2VBText.Text +story Mod userPicks.ToString + vbNewLine Python2VBText.MultiLine = True Python2VBText.Width = 200 Python2VBText.Height = 100 Form1.Controls.Add(Python2VBText) End Sub Public Sub main() Dim originalStory As String originalStory = "Once upon a time, deep in an ancient jungle," + vbNewLine + "there lived a Mod (animal)s. This %(animal)s" + vbNewLine + "liked to eat %(food)s, but the jungle had" + vbNewLine + "very little %(food)s to offer. One day, an" + vbNewLine + "explorer found the %(animal)s and discovered" + vbNewLine + "it liked %(food)s. The explorer took the" + vbNewLine + "%(animal)s back to %(city)s, where it could" + vbNewLine + "eat as much %(food)s as it wanted. However," + vbNewLine + "the %(animal)s became homesick, so the" + vbNewLine + "explorer brought it back to the jungle," + vbNewLine + "leaving a large supply of %(food)s." + vbNewLine + "" + vbNewLine + "The End" + vbNewLine + "" tellStory(originalStory) End Sub 'main()