๐Ÿค” Setting Up the Framework for Choices | myBetabox

๐Ÿค” Setting Up the Framework for Choices

Keyboard Guide

Setting Up the Framework for Choices

Stuck? Copy & Paste the Code into Repl.it

class TreeNode:
  def __init__(self, story_piece):
    self.story_piece = story_piece
    self.choices = []

  def add_choices(self, choice_a, choice_b):
    self.choices.append(choice_a)
    self.choices.append(choice_b)

The Code Explained

Yourย nodes are the parts of your story where the reader makes a choice. For example, your node could be

The brave astronaut lands on a mysterious planet. Does she go to the left or to the right?

The story_piece acts the same way. Each part of the story is its own piece of the adventure, kind of like chapters. We are identifying that this node is the starting point of the story.

   self.story_piece = story_piece

Next we create aย list that contains all our story options. Does the astronaut go left or right? You can think of lists like items on a shopping list!

๐ŸŒŸย Lists allow you to store different types of data in an order. Lists use square brackets and the data inside are separated by commas.

    self.choices = []

We will type out our actual choices for this list in the next section but for now let’s create a function so we can do that.

    def add_choices(self, choice_a, choice_b):
      self.choices.append(choice_a)
      self.choices.append(choice_b)

Theย function add_choices() will add our story options for us. Going left could be choice_a and going right could be choice_b. Addingย .append to the function will add the choices to the end of the list.

For example, if you had a shopping list called self.shopping = [apple, banana, milk] and you entered self.shopping.append(bread) then your list will add bread to the end of the list [apple, banana, milk, bread].