Interactive Fiction
Objectives
- Get comfortable working with code branching using
if,elif, andelse. - Begin working in larger and more complex
pythonfiles.
Background
For this problem, you—the author–should create a Python program (built on the starting template below) that is a work of interactive fiction, a.k.a., a choose-your-own-adventure program. It needs to include at least 5 if-based control structures, as noted below:
An example from which to build
Start by creating a file, we will call it fiction.py, and pasting this skeleton of a story into it:
Try out the above story by
- Opening the integrated terminal in vscode
cdto the directory wherefiction.pyis located- Running
python fiction.py
Once you’ve done that, try it at least twice to show the different paths through the story.
The Exercise
You’re ready to go! Partner up with someone, if you’d like (entirely optional!) and create your own adventure.
Certainly feel free to modify or continue the poptart-related story—or, if you wish, do something completely different to compose your own work of interactive fiction. The story above includes two
if-based control structures: (a) anif-elif-elseand (b) anif-else.The requirement of this problem is that you use each of five conditional control structures in a way that affects the story or dialog. They don’t have to appear in this order, and you’re welcome to have many more than this if you wish!
Here are the control structures to be sure to include:
- An
if,elif, andelsecontrol structure (with exactly oneelif)- An
if,elif,elif, … andelsecontrol structure (with at least twoelifs)- An
if,elsecontrol structure (with zeroelifs)- An
if,elif, … control structure (with one or moreelifsbut no trailingelseat all)- An
ifcontrol structure (with no trailingelifnor trailingelseat all)Please keep things light! There are forums for all forms of expression—this one’s for contextualizing conditionals in CS.
So, keep in mind that, potentially, lots of people (our coaches!) will read—and run—your story (we look forward to it!) Also, please be mindful of the time you spend! The goal here is not to take a lot of time. (It’s easy to let time slip away: the poptart adventure above took much longer than I originally thought it would—and it has only one control structure!)
When you’re done
When you finish this exercise, raise your hand to call over a coach if you would like someone to go through your solution with you!