3 Selection and branching code.

Learning Objectives

  • Understand the need for branching in programs.
  • Practice branching with interactive stories in twine.
  • Learn the syntax to create branching code in Python

Learning Outcomes

All must Test a text based adventure, and create a short and simple adventure story using the Twine software on line (Level 5)

Most should complete the above, and create/adapt a simple branching program in Python. (Level 5)

Some could complete the above, creating a branching program in python without any help, using if/else and also nested if statements. (Level 6)


Words to learn: Selection,  branching, if/else,  nesting


We’ll start off with a game.   Published in 1979, Zork was a tremendous success amongst students and people lucky enough to have access to a computer.  The game is entirely text-based, as computers were nowhere near powerful enough to display graphics, showing different scenes.   Zork was a combination of puzzle and fantasy adventure.  By typing instructions in simple English, you should be able to find your way around the game.

Have a go by clicking here.

In your book, jot down a couple of sentences giving your opinions of the game.  Do you like it? Was it easy? Confusing?  Did you achieve much in the time you had?


You’ll see that you were able to make choices based on the text you were given in the game.  As such, the story is shaped around your decisions.  The correct term for this is branching.

Click here to visit Twine.  This is a wonderful tool for creating interactive branching fiction.
Click “Use it online” on the right-hand side,  then the green “+Story” box also on the right.

You’ll need to give your story a name.  Following on from Zork, let’s call it “Lost in the woods”.
python3aNow you’ll see a box like this one.  Double-click on it, to open a text window.  Call it “Lost”.





Good.  Now add this text:

I don’t know what went wrong.  This started out as a pleasant evening stroll through the woods,  but you must have taken a wrong turn somewhere. The light is fading quickly, and you’ve no idea where you are.  There is a [[shady path to the left]],  and you’re sure you can see some kind of [[flickering light ahead]].

Now close the text box and look.  Where you added the square brackets,  new story boxes have been created and given titles.  If you click play, below, you can see that the first paragraph appears with hyper links, letting you navigate through the story.


  1. Continue with the story. Add as many story boxes as you can, and let your imagination run wild.

Improve your story:

  1. You can create links back to earlier parts of the story,  by typing the name of any existing box between square brackets.
  2. Your link doesn’t have to be the name of the story box.
    [[some text -> name of box]]
    Lets you write something different from the name of the box you’re linking to.
  3. *Some text*  will give you italics.
    **more text** will give you bold.
    <u>even more text</u> will underline text.

Back to Python

We’ll come back to Twine later, but for now we need to see how this fits in with Python.

When writing a program, we come to a point where we have to make decisions based on data and values within our code.

Timetable checker…

Now we’re going to write a program to check our timetable. Get out your planner, have a look at your timetable, and find a lesson you have on monday…  put it into this code…


Run the program.  So far, it asks you for a day of the week.  If you type “Monday”, it’ll tell you one lesson you have that day.

**Note.  If is always followed by a “condition”  if this is true then the indented code underneath will run.  If not, it won’t run, and the program will continue.**

So far, this isn’t much use.  Our program only knows about one day of the week, and doesn’t respond to anything else.  Let’s give it an error message…