Python Activities#

The first group of collaborative activities give students opportunities to apply foundational Python concepts, using a madlib generator, a coin change calculator, and an ASCII Art program.

Mad Lib Generator#

A mad lib is a word game that prompts you to supply words that are added to a template to generate a story.

madlib example

In a traditional analog mad lib, one person has the template, another person supplies the words, and hilarity ensues.

We can use the input() & print() functions, along with concatenation, to implement a mad lib generator in Python.

There are lots of mad lib story ideas online, and folks are welcome to come up with their own. But we could also use a work of literature, specifically a poem, as a mad lib template.

For example, we can use Python to come up with a mad lib version of a stanza from Byron’s “She Walks in Beauty.”

She walks in beauty, like the night Of cloudless climes and starry skies; And all that’s best of dark and bright Meet in her aspect and her eyes; Thus mellowed to that tender light Which heaven to gaudy day denies.

One shade the more, one ray the less, Had half impaired the nameless grace Which waves in every raven tress, Or softly lightens o’er her face; Where thoughts serenely sweet express, How pure, how dear their dwelling-place.

And on that cheek, and o’er that brow, So soft, so calm, yet eloquent, The smiles that win, the tints that glow, But tell of days in goodness spent, A mind at peace with all below, A heart whose love is innocent!

Let’s start by mapping out parts of speech for the first stanza.

Tagged parts of speech

Now that we have a better sense of how parts of speech are working in the poem, we can use the input() & print() functions, along with concatenation, to reimagine the poem as a mad lib.

Let’s look at a sample program:

# start program
print("We're going to create a mad lib love poem!")

# user inputs
verb1 = input("Enter a verb: ")
noun1 = input("Enter a noun: ")
adj1 = input("Enter an adjective: ")
noun2 = input("Enter another noun: ")
adj2 = input("Enter another adjective: ")
noun3 = input("Enter another noun: ")

# poem madlib
print("She " + verb1 + " in beauty, like the " + noun1 + "\n Of " + adj1 + " " + noun2 + " and " + adj2 + " " + noun3)

Step #1: Now it’s your turn! As a group, decide on the template for your mad lib.

Step #2: Once you have a mad lib template, start to develop a pseudocode outline for your program

  • Start with a word problem, or a narrative description of the task

    • What is the goal

    • What are the parameters or constraints

    • Any particular commands or syntax you’re asked to use

  • Start breaking down the tasks into steps, thinking about how programming languages (specifically Python) approach logic

    • Input/output, variables, operators (arithmetic, comparison)

    • What are the programming building blocks that connect to aspects of the task

    • BUT still write things out in plain language (not programming syntax)

  • Continue breaking the task into steps

    • The goal is one programming step (or statement) per line

Step #3: Now we’re ready to start writing the Python program! Leverage the group roles to start translating your pseudocode outline into a Python program.

Step #4: Continue to work on debugging and testing your program, switching up group roles as needed.

Step #5: Spend a few minutes as a group discussing and reflecting on the experience. What challenges did your group encounter, and how did you solve them? Other comments or observations?

Coin Change Calculator#

One of this week’s application questions asked you to write a program that calculates the fewest number of U.S. coins needed to make change for a 73 cent remainder. To put that another way- write a program that determines the smallest number of coins in each U.S. denomination needed to make up 73 cents. Answer to this question includes program + comments that document process and explain your code.

In U.S. currency, one dollar ($1.00) equals one hundred (100) cents. The coins used to make change have a face value in cents.

  • Quarter = 25 cents

  • Dime = 10 cents

  • Nickel = 5 cents

  • Penny = 1 cent

Now, let’s write a program that asks the user to input the amount of change your program is to make, and then compute the number of quarters, dimes, nickels, and pennies that produces the correct change with the fewest coins possible.

Step #1: Start to develop a pseudocode outline for your program.

  • Start with a word problem, or a narrative description of the task

    • What is the goal

    • What are the parameters or constraints

    • Any particular commands or syntax you’re asked to use

  • Start breaking down the tasks into steps, thinking about how programming languages (specifically Python) approach logic

    • Input/output, variables, operators (arithmetic, comparison)

    • What are the programming building blocks that connect to aspects of the task

    • BUT still write things out in plain language (not programming syntax)

  • Continue breaking the task into steps

    • The goal is one programming step (or statement) per line

Step #2: Now we’re ready to start writing the Python program! Leverage the group roles to start translating your pseudocode outline into a Python program.

Step #3: Continue to work on debugging and testing your program, switching up group roles as needed.

Step #4: Spend a few minutes as a group discussing and reflecting on the experience. What challenges did your group encounter, and how did you solve them? Other comments or observations?

ASCII Art Creation#

“ASCII art is a graphic design technique that uses computers for presentation and consists of pictures pieced together from the 95 printable (from a total of 128) characters defined by the ASCII Standard” (Wikipedia, “ASCII art”)

An example of ASCII art output:

           * *
          *   *
           * *
            *
          * * *
         *  *  *
        *   *   *
            *
          *   *
         *     *
        *       *
       *         *
      *           *

Write a program that creates your own ASCII art. You’re welcome to consult the ASCII Art Archive for inspiration.

One option to create this output in Python is to use multiple print statements. You can also use the regular expressions \t or \n to add tabs (spaces) and new lines (respectively).

Step #1: Start to develop a pseudocode outline for your program.

  • Start with a word problem, or a narrative description of the task

    • What is the goal

    • What are the parameters or constraints

    • Any particular commands or syntax you’re asked to use

  • Start breaking down the tasks into steps, thinking about how programming languages (specifically Python) approach logic

    • Input/output, variables, operators (arithmetic, comparison)

    • What are the programming building blocks that connect to aspects of the task

    • BUT still write things out in plain language (not programming syntax)

  • Continue breaking the task into steps

    • The goal is one programming step (or statement) per line

Step #2: Now we’re ready to start writing the Python program! Leverage the group roles to start translating your pseudocode outline into a Python program.

Step #3: Continue to work on debugging and testing your program, switching up group roles as needed.

Step #4: Spend a few minutes as a group discussing and reflecting on the experience. What challenges did your group encounter, and how did you solve them? Other comments or observations?