Mad Lib Example

From Knowledge Kitchen
Jump to navigation Jump to search


Example Python code

Note the use of the ''' triple-quote string delimiters that allow you to write line breaks in your string in a natural way. Also note the use of the format() function that allows you to write one big string for the entire poem.

#
#Assignment #1 - Mad Lib
#Foo Barstein
#foo.barstein@foobarstein.com
#

print("Welcome to the Mad Lib Generator!!!\n")

noun1 = input("Please enter a noun: ")
noun2 = input("Please enter a plural noun: ")
adj1 = input("Please enter an adjective: ")
adj2 = input("Please enter an adjective: ")
adv1 = input("Please enter an adverb: ")
adv2 = input("Please enter an adverb: ")
num1 = input("Please enter an number: ")
num2 = input("Please enter a number higher than 4: ")
  
mad_lib =   """
            'Twas {n1}, and the {a1} {n2}
            Did gyre and gimble in the wabe;
            All {a2} were the borogoves,
            And the {num1} raths {adv1} outgrabe.
            "Beware the Jabberwock, my {num2}th son!
            The jaws that bite, the claws that  {adv2} catch!
            Beware the Jubjub bird, and shun
            The frumious Bandersnatch!"
            """.format(n1=noun1, n2=noun2, a1=adj1,  a2=adj2, adv1=adv1, adv2=adv2,  num1=num1, num2=num2)

print("\nHere is your mad lib:\n")

print(mad_lib)

Example Javascript code

For your reference, here is the same code in Javascript, a different high-level programming language. This will run if you open it in a web browser, since web browsers all have built-in Javascript interpreters.

<script>

document.write("Welcome to the Mad Lib Generator!!!\n")

noun1 = prompt("Please enter a noun: ")
noun2 = prompt("Please enter a plural noun: ")
adj1 = prompt("Please enter an adjective: ")
adj2 = prompt("Please enter an adjective: ")
adv1 = prompt("Please enter an adverb: ")
adv2 = prompt("Please enter an adverb: ")
num1 = prompt("Please enter an number: ")
num2 = prompt("Please enter a number higher than 4: " )

mad_lib = "'Twas " + noun1 + ", and the " + adj1 + "  " + noun2 + "\n Did gyre and gimble in the wabe;\ nAll " + adj2 + " were the borogoves,And the " +  num1 + " raths " + adv1 + " outgrabe.\"Beware  the Jabberwock, my " + num2 +"th son!The jaws  that bite, the claws that " + adv2 + "  catch!Beware the Jubjub bird, and shun\nThe  frumious Bandersnatch!\""

document.write("\nHere is your mad lib:\n")

document.write(mad_lib)

</script>


What links here