Python Activities#
The first group of collaborative activities give students opportunities to apply foundational Python concepts, using a madlib generator, a mascot guessing game, and color guessing game.
Mascot Guessing Game#
Write a program that asks a user to input their current or former Notre Dame residence hall and returns or outputs that hall’s mascot. How might the data structures covered in this chapter be useful?
Residence Hall Mascots
Fuzzy on your ND mascot trivia? Wikipedia has you covered.
Sample output for this program:
Howard Hall's mascot is the ducks!
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?
Color Guessing Game#
Write a program that asks the user to enter a color value and returns an output message indicating whether the color is a primary, secondary, or tertiary color. How might the data structures covered in this chapter be useful?
Hint
Your group might also find it useful to think about comparison operators, if-then-else logic, and Boolean logical operators.
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?
Mad Lib Generator#
Last week, folks had the chance to build their own mad lib. A mad lib is a word game that prompts you to supply words that are added to a template to generate a story. In a traditional analog mad lib, one person has the template, another person supplies the words, and hilarity ensues.
We can return to that workflow, incorporating this chapter’s data structures. For example, instead of just making single word substitutions, we could have groups of terms and make random selections from the group.
Let’s look at another version of our Byron example:
import random # import random module
terms = {
'verbs': ['runs', 'crawls', 'begs', 'rides', 'sings'], # list of verbs
'prepositions': ['in', 'on', 'under', 'over', 'through'], # list of prepositions
'adjectives' : ['small', 'warm', 'angry', 'happy'], # list of adjectives
'nouns' : ['cloud', 'terrace', 'castle', 'woods']
}
# poem madlib
print("She " + random.choice(terms['verbs']) + " in beauty, like the " + random.choice(terms['nouns']) + "\n Of " +
random.choice(terms['adjectives']) + " " + random.choice(terms['nouns']) + " and " + random.choice(terms['adjectives']) + " " + random.choice(terms['nouns']))
Step #1: As a group, decide on the template for your mad lib.
Poetry is one option, and the Poetry Foundation’s website is a great starting point.
If you want to experiment with a generative AI tool (i.e. ChatGPT), that’s also fair game to get a story template.
Song lyrics can also be a good genre (even the Notre Dame Victory March)
But your group can also create your own!
Note
While folks are welcome to write a mad lib that takes inputs from the user, you’re also welcome to focus on text generation in Python and create your own lists of terms to draw on.
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?
Creative Text Generation#
Want to try a different form of creative text generation? Since the early days of computers, artists and writers have been thinking about how computers might facilitate different forms of text generation and creative expression.
Dadaist Poem#
Avant-garde 20th century Poet Tristan Tzara developed a framework for making a Dadaist poem (circa 1920). We can implement a similar approach in Python using list methods. The program below is based on Allison Parrish’s implementation, but you can modify the original text and output format to make your own creation!
# based on: https://www.writing.upenn.edu/~afilreis/88v/tzara.html
import random, textwrap # import Python modules
# paragraph of text
newspaper = """
Dada was an informal international movement, with
participants in Europe and North America. The
beginnings of Dada correspond to the outbreak of
World War I. For many participants, the movement
was a protest against the bourgeois nationalist
and colonialist interests, which many Dadaists
believed were the root cause of the war, and
against the cultural and intellectual
conformity — in art and more broadly in
society — that corresponded to the war."""
words = newspaper.split() # split newspaper variable into list of words
random.shuffle(words) # shuffle list of words
print(textwrap.fill(" ".join(words), 60)) # recreate paragraph from shuffled list
House of Dust#
Alison Knowles and James Tenney were American artists associated with the Fluxus in the mid-20th century. Their 1967 poem “A House of Dust”, originally written in FORTRAN, was one of the first computational poems. Folks can watch a video of Knowles reciting the poem!
The program below is a Python implementation of the original FORTRAN program, based on Allison Parrish’s and Nick Montfort’s work. But you can modify the original text and output format to make your own creation!
# based on: https://github.com/aparrish/rwet/blob/master/some-poetry-generators.ipynb & https://nickm.com/memslam/the_house_of_dust.html
import random # import random
materials = [ # list of materials
'brick',
'broken dishes',
'discarded clothing',
'dust',
'glass',
'leaves',
'mud',
'paper',
'plastic',
'roots',
'sand',
'steel',
'stone',
'straw',
'tin',
'weeds',
'wood'
]
locations = [ # list of locations
'among high mountains',
'among other houses',
'among small hills',
'by a river',
'by an abandoned lake',
'by the sea',
'in a cold, windy climate',
'in a deserted airport',
'in a deserted church',
'in a deserted factory',
'in a green, mossy terrain',
'in a hot climate',
'in a metropolis',
'in a place with both heavy rain and bright sun',
'in an overpopulated area',
'in dense woods',
'in heavy jungle undergrowth',
'in japan',
'in michigan',
'in southern france',
'inside a mountain',
'on an island',
'on the sea',
'underwater'
]
lights = [ # list of lighting options
'all available lighting',
'candles',
'electricity',
'natural light'
]
inhabitants = [ # list of inhabitant options
'all races of men represented wearing predominantly red clothing',
'children and old people',
'collectors of all types',
'fishermen and families',
'french and german speaking people',
'friends',
'friends and enemies',
'horses and birds',
'little boys',
'lovers',
'people from many walks of life',
'people speaking many languages wearing little or no clothing',
'people who eat a great deal',
'people who enjoy eating together',
'people who love to read',
'people who sleep almost all the time',
'people who sleep very little',
'various birds and fish',
'vegetarians',
'very tall people'
]
stanza_count = 7 # number of stanzas for the poem
for i in range(stanza_count): # create number of stanzas
print()
print("A house of " + random.choice(materials))
print(" " + random.choice(locations))
print(" using " + random.choice(lights))
print(" inhabited by " + random.choice(inhabitants))