Learn Trees: "Choose Your Own Adventure: Wilderness Escape" Project


#1

I am pretty deep into this pro+ project, however I am stuck at a point where when I call choice_a.add_child(choice_a_1) and choice_a.add_child(choice_a_2) the program stays on choice_a and choice_b instead, meaning that it will not allow me to progress in the game to choice_a_1 and choice_a_2

In other words, my program does not go down to the next node in the tree and treats both choices a and b as the leaves.

How do I go about fixing this?

######
# TREENODE CLASS
######
class TreeNode:
  def __init__(self, story_piece):
    self.story_piece = story_piece
    self.choices = []
  def add_child(self, node):
    self.choices.append(node)
  def traverse(self):
    story_node=self
    print(story_node.story_piece)
    while len(self.choices) != 0:
      choice = input("Enter 1 or 2 to continue the story ")
      chosen_index = int(choice)
      chosen_index -= 1
      chosen_child = self.choices[chosen_index]
      if chosen_child in self.choices:
        print('\n'+chosen_child.story_piece+'\n')
        story_node=chosen_child
      else:
        print("Make another selection.")
      
######
# VARIABLES FOR TREE
######
story_root = TreeNode("You are in a forest clearing. There is a path to the left. \nA bear emerges from the trees and roars! \nDo you:  \n 1 ) Roar back! \n 2 ) Run to the left...")
user_choice = input("What is your name?\n ")

choice_a = TreeNode("The bear is startled and runs away. \n Do you: \n 1 ) Shout 'Sorry bear!' \n 2 ) Yell 'Hooray!'")
choice_b = TreeNode("You come across a clearing full of flowers. \n The bear follows you and asks 'what gives?'\n Do you: \n 1 ) Gasp 'A talking bear!' \n 2 ) Explain that the bear scared you.")
story_root.add_child(choice_a)
story_root.add_child(choice_b)

choice_a_1= TreeNode("The bear returns and tells you it's been a rough week. After making peace with a talking bear, he shows you the way out of the forest. \n YOU HAVE ESCAPED THE WILDERNESS.")
choice_a_2= TreeNode("The bear returns and tells you that bullying is not okay before leaving you alone in the wilderness.\n YOU REMAIN LOST.")
choice_a.add_child(choice_a_1)
choice_a.add_child(choice_a_2)
######
# TESTING AREA
######
print("Once Upon a Time...")
#print(story_root.story_piece)
print(story_root.traverse())

#2

P.S. I haven’t given the if clause any legitimate redundancy checks yet.


#3

Hi @jhern603,

What object does self always refer to in this statement within the traverse method, as the while loop iterates? …

      chosen_child = self.choices[chosen_index]

#4

It’s referring to the variable “choices” which is in the __init__ constructor, which otherwise is defined per the instance of where choices is being used. Without the self. I get a NameError


#5

image


#6

You do need to reference a TreeNode object in order to make the necessary method calls and other accesses to information. However, in the traverse method, as we were asked to write it, self always refers to the root of the tree.

This statement establishes the story_node variable as a means of keeping track of the current TreeNode as we perform the traversal …

    story_node=self

Thereafter, within the traverse method, you should refer to story_node wherever you are currently referring to self.