↔️ Adding the Next Story Choices | myBetabox

↔️ Adding the Next Story Choices

Keyboard Guide

Adding the Next Story 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)

  def tell_story(self):
    story_node = self
    print(story_node.story_piece)
    while len(story_node.choices) !=0:
      answer = int(input("Enter 0 or 1 to continue the story: "))
      if answer == 0 or answer == 1:
        choice = story_node.choices[answer]
        story_node = choice
        print(story_node.story_piece)
      else:
        print("Invalid answer! Try again!")

story_root = TreeNode("You awake in an empty room. There is a phone booth in the middle. \nDo you: \n 0 ) Investigate the phone booth. \n 1 ) Leave it alone.\n")
current_node = story_root
choice_a = TreeNode("You enter the phone booth and look around. You see a button that says 1980 and a button that says 3000. \nDo you: \n 0 ) Press 1980. \n 1 ) Press 3000.\n")
choice_b = TreeNode("You decide to leave the phone booth alone. You live out the rest of your life in boredom.\n")

The Code Explained

We have to update the current_node variable with the node we are now on.

current_node = story_root

Next, let’s add in our official first story options! Note how these choices are technically nodes themselves which is why we have them set to TreeNode(). Type out your scenario as a string including your two prompts.

choice_a = TreeNode("You enter the phone booth and look around. You see a button that says 1980 and a button that says 3000. \nDo you: \n 0 ) Press 1980. \n 1 ) Press 3000.\n")
choice_b = TreeNode("You decide to leave the phone booth alone. You live out the rest of your life in boredom.\n")

Choice_a is what happens when we investigate the booth. Choice_b is what happens when we leave it alone. You’ll notice that choice_b leads to a dead end.

You could make choice_a your dead end if you wish but keep track of which choices continue the story and which do not.