Skip to main content
Engineering LibreTexts

12.2: Assignment

  • Page ID
    26345
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Now for a little fun. This exercise will be a take off on Mad Libs, which is a word substitution game. The idea is to create randomized phrases based on a sentence template and lists of words to be substituted into the template. For example, the template might be “I am a adjective noun”. The list of adjectives might be items such as green, large, and puffy. The list of nouns might include man, warthog, and car. An item from each list is chosen randomly and inserted into the template. We might wind up with phrases such as “I am a green warthog” or “I am a puffy man”. The longer the sentence and the larger the lists, the greater the number of resulting permutations. Some of these permutations will be unexpectedly humorous.

    This variation is commonly known as Curses (in this case, Curses.py). It will generate wild, non-profane curses. While it is very easy to utter a common “four letter word” curse, they tend to be boring as we’ve all heard them before. Curses instead seeks to create novel phrases that will cause someone to stop for a moment and consider what has just been said.

    One of the keys to good version of Curses is the curse template. You can come up with one by simply creating a unique curse and identifying nouns, adjectives, and so forth for substitution. Consider for example the following phrase an impulsive driver might yell after being cutoff in traffic: “You dirty little weasel! I hope your stupid car breaks!” Here’s a template:

    “You adjective adjective noun! I hope your adjective noun verb!”

    A list of adjectives might include obvious items such as “little” and “dirty”, but it can also include less obvious (and hopefully humorous in context) items such as “worm-infested”, “vomit-covered” or “odiferous”. Likewise, verbs such as “breaks” can also give way to short phrases such as “gyrates madly” or “turns gangrenous”. Nouns, of course, would be given a similar treatment, perhaps with items such as “washing machine” or “younger sister”. So, after substitution, we might end up with a curse such as “You worm-infested, vomit-covered washing machine! I hope your younger sister turns gangrenous!” That phrase will surely make the offending driver do a double-take.

    For further fun enhancement, we can make this version of curses create several curses instead of just one. Considering all of the above, we can create tuples to hold strings for each of the items to be substituted (i.e., a tuple of nouns, one of verbs, etc.). These can then be accessed with a simple random integer and printed out to form the sentences of the curse. The random integer generation and print out can be wrapped inside of a loop to generate several different curses.

    Here’s a possible pseudo code:

    1. Don’t forget to import the random module (import random).

    2. Define tuples for nouns, verbs, etc.

    3. Give the user directions.

    4. Ask user for number of curses desired.

    5. Start a loop for the number of curses.

    6. Generate a random number for each of the items to be substituted.

    7. Print out the curse using the random numbers to access an element of the appropriate tuple.

    8. End of loop from step five.

    Definition of the tuples in step one is the major bit of effort in this program. DO NOT resort to common four letter words. That completely ruins the effect of the curse and will result in a downgrading of the assignment. If need be, consult a thesaurus.

    Here is a short example of some possible adjectives:

    adj = ('green', 'dirty', 'vomit-covered', 'self-loathing')
    

    It is suggested that the tuples be at least 10 items in length (preferably twice that) because the longer the tuple, the greater the number of possibilities.

    In step three, the number of curses will have to be an integer. If we call this NumCurses, we can set up a for loop in step four as follows:

    for Curse in range( NumCurses ):
    

    Inside of this loop we will have to generate a bunch of random numbers, one to index each slot that needs a substitution. randrange() is ideal for this as it creates integers that can then be used to access the tuples. If there are 15 items to choose from in the tuple, the following line would do the trick:

         i1 = random.randrange(0,15)
    

    Of course, you could also use the len() function which can be handy if you’re editing the tuples to add items:

         i1 = random.randrange(0,len(adj))
    

    This would be repeated for as many indices as needed for your template. At this point, we’re at step six, printing out the curse itself. This simply involves a mix of fixed strings and indexed tuples, something like the line below:

        print( "You", adj[i1], adj[i2], noun[i3]+"!" )
    

    This page titled 12.2: Assignment is shared under a not declared license and was authored, remixed, and/or curated by James M. Fiore.

    • Was this article helpful?