Week 1 Exercise: Choose Your Own Adventure

Chris Tralie, heavily inspired by Bill Mongan

Overview / Logistics

The purpose of this exercise is to give you practice with python dictionaries, which will be very important during the second unit of our class, and which are important in many python data wrangling tasks in general. The idea for this exercise was inspired by an assignment by Bill Mongan.

Learning Objectives

  • Practice declaring variables, strings, and lists in python
  • Use proper data structures as keys and values in python dictionaries
  • Properly index python arrays
  • Choose an appropriate loop structure for a given task

Background

Choose Your Own Adventure books are a series of books from the '80s and '90s in which the reader takes a more active than usual role. In particular, the books are written in the second person, and they turn over key decisions to the reader. Based on these decisions, the reader jumps to different pages to see the outcome of those decisions.

In this exercise, you'll be designing a Choose Your Own Adventure-style program to tell a dynamic story. There are four pieces of information that you should represent in your program

  1. A list of places you can be in your world
  2. A little blurb describing each place
  3. A list of places you can travel to next from each place
  4. Which place you start, and which place ends

Your Below is a very simple example to illustrate this idea, which pretty much sums up my life as an instructor during quarantine last year (thank goodness those days are over for now...). The game starts in the office, and it ends whenever someone reaches the outside.

A sample run through the program might look like this

  • You are in the Office. The place where you frantically try to get everything ready for class. Where would you like to go?

    Options: [Kitchen, Bathroom]

    Input: Kitchen

  • You are in the Kitchen. The place where you heat up frozen food and sometimes (but rarely) cook. Where would you like to go?

    Options: [Office, Upstairs Bedroom, Outside]

    Input: Upstairs Bedroom

  • You are in the Upstairs Bedroom. The place you go rarely when you've finished your work for the day. Where would you like to go?

    Options: [Kitchen, Upstairs Bathroom]

    Input: Kitchen

  • You are in the Kitchen. The place where you heat up frozen food and sometimes (but rarely) cook. Where would you like to go?

    Options: [Office, Upstairs Bedroom, Outside]

    Input: Upstairs Bedroom

  • ......

Programming Task

Data Model

You should create a dictionary called places as follows:

  • Keys are strings with the names of the different places you can go in your story
  • The values associated to different keys should be lists of strings:
    • The first element in this list should be a description of the place
    • The remaining elements in this list should be the strings of the places you can reach from this place
    Alternatively, the values can be dictionaries themselves, with a key for the description and a key for the list of places one can go next

The example I gave is pretty boring (because it mirrors my life!). But you should feel free to be as creative as you want. It is recommended that you sketch your world out of paper before you translate it into code.

Game Loop

Your program should go through a loop where in each iteration, it prints the place you are and its description, followed by a list of places you can go. Then, you should wait for input from the user to say which place to go. Finally, you should have a constant string which represents where the loop starts, and a constant string to represent when the loop should terminate.

Have fun!


Below is a solution to this problem:


Student Adventures

Below are a few stories that students came up with in class. Click through them to explore!

NOTE: Evan's and James's stories didn't quite fit into the format because, in addition to places one can go next, they also included different actions! Click here to see Evan's code, and Click here to see James's code!

Choose Adventure
Location Description Next Step