Choose Your Own Adventure: Wilderness Escape - Learn Complex Data Structures

I have this code:

print("Once upon a time...")

######

# 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)

######

# VARIABLES FOR TREE

######

story_root = TreeNode

(

"""

You are in a forest clearing. There is a path to the left.

A bear emerges from the trees and roars!

Do you: 

1 ) Roar back!

2 ) Run to the left...

"""

)

choice_a = TreeNode

(

"""

The bear is startled and runs away.

Do you:

1 ) Shout 'Sorry bear!'

2 ) Yell 'Hooray!'

"""

)

choice_b = TreeNode

(

"""

You come across a clearing full of flowers. 

The bear follows you and asks 'what gives?'

Do you:

1 ) Gasp 'A talking bear!'

2 ) Explain that the bear scared you.

"""

)

######

# TESTING AREA

######

#print(story_root.story_piece)

user_choice = input("What is your name? ")

# print(user_choice)

story_root.add_child(choice_a)

story_root.add_child(choice_b)

story_root.traverse()

This is the output on Bash:

Once upon a time...
What is your name? Anonymous
Traceback (most recent call last):
  File "script.py", line 60, in <module>
    story_root.add_child(choice_a)
TypeError: add_child() missing 1 required positional argument: 'node'

I was unable to copy the output so I had to type it all by myself.
How do I fix the error(someone, @mtf)?
Edit:
There is nothing wrong. I decided to use exception handing in the code to find out where the errors are:

print("Once upon a time...")

######
# 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)

######
# VARIABLES FOR TREE
######

story_root = TreeNode
(
"""
You are in a forest clearing. There is a path to the left.
A bear emerges from the trees and roars!
Do you: 
1 ) Roar back!
2 ) Run to the left...
"""
)
choice_a = TreeNode
(
"""
The bear is startled and runs away.
Do you:
1 ) Shout 'Sorry bear!'
2 ) Yell 'Hooray!'
"""
)
choice_b = TreeNode
(
"""
You come across a clearing full of flowers. 
The bear follows you and asks 'what gives?'
Do you:
1 ) Gasp 'A talking bear!'
2 ) Explain that the bear scared you.
"""
)

######
# TESTING AREA
######

#print(story_root.story_piece)
user_choice = input("What is your name? ")
# print(user_choice)

try:
  story_root.add_child(choice_a)

except TypeError:
  print("TypeError")

try:
  story_root.add_child(choice_b)

except TypeError:
  print("TypeError")

try:
  story_root.traverse()

except TypeError:
  print("TypeError")

Output:

TypeError
TypeError
TypeError

I’ve figured it out this interpreter doesn’t interpret self as the name of the class because it assumes that the passed in parameter correspond to self. This is so strange!!! Can you help me? Here is the link to the exercise: https://www.codecademy.com/courses/complex-data-structures/projects/wilderness-escape

You can’t use syntax like this for a function call-

story_root = TreeNode
(

That new line has meaning to the parser and since there’s no implicit or explicit line joining those are treated as different logical lines. So you actually have story_root = Tree_node and then separately there’s a string which is never assigned. You need to get rid of the newline for a proper call. The following is valid for what I think you intended-

story_root = TreeNode(
""" Stuff ...
1 Like

Is this valid:

story_root = TreeNode(
"""
You are in a forest clearing. There is a path to the left.
A bear emerges from the trees and roars!
Do you: 
1 ) Roar back!
2 ) Run to the left...
""")

To make things clearer, how does python allow me to put the multi line string on a different line to the first bracket? Can I put the last bracket one line to the bottom like this:

story_root = TreeNode(
"""
You are in a forest clearing. There is a path to the left.
A bear emerges from the trees and roars!
Do you: 
1 ) Roar back!
2 ) Run to the left...
"""
)

What if you use this. Is it valid?

story_root = \
( \
"""
You are in a forest clearing. There is a path to the left.
A bear emerges from the trees and roars!
Do you: 
1 ) Roar back!
2 ) Run to the left...
""" \
)

Anyway now I am almost finished the project: 38 / 39; I could tick the last one but I want the problem to solved(actually already solved).
Edit:
To let you know now this is my code:
script.py


######
# 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(story_node.choices) > 0:
      choice = input("Enter 1 or 2 to continue the story: ")
      if choice not in ["1", "2"]:
        choice = input("Please enter 1 or 2. ")
      if choice in ["1", "2"]:
        choosen_index = int(choice) - 1
        choosen_child = story_node.choices[choosen_index]
        print(choosen_child.story_piece)
        story_node = choosen_child


######
# VARIABLES FOR TREE
######

story_root = TreeNode
(
"""
You are in a forest clearing. There is a path to the left.
A bear emerges from the trees and roars!
Do you: 
1 ) Roar back!
2 ) Run to the left...
"""
)

choice_a = TreeNode
(
"""
The bear is startled and runs away.
Do you:
1 ) Shout 'Sorry bear!'
2 ) Yell 'Hooray!'
"""
)

choice_b = TreeNode
(
"""
You come across a clearing full of flowers. 
The bear follows you and asks 'what gives?'
Do you:
1 ) Gasp 'A talking bear!'
2 ) Explain that the bear scared you.
"""
)

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.
 
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.
 
YOU REMAIN LOST.
"""
)

choice_b_1 = TreeNode
(
"""
The bear is unamused. After smelling the flowers, it turns around and leaves you alone.
 
YOU REMAIN LOST.
"""
)

choice_b_2 = TreeNode
(
"""
The bear understands and apologizes for startling you. Your new friend shows you a 
path leading out of the forest.
 
YOU HAVE ESCAPED THE WILDERNESS.
"""
)
######
# TESTING AREA
######

#print(story_root.story_piece)
user_choice = input("What is your name? ")
# print(user_choice)

try:
  story_root.add_child(choice_a)

except TypeError:
  print("TypeError")

try:
  story_root.add_child(choice_b)

except TypeError:
  print("TypeError")

try:
  story_root.traverse()

except TypeError:
  print("TypeError")

try:
  story_root.traverse()

except TypeError:
  print("TypeError")

try:
  choice_a.add_child(choice_a_1)

except TypeError:
  print("TypeError")

try:
  choice_a.add_child(choice_a_2)

except TypeError:
  print("TypeError")

try:
  choice_b.add_child(choice_b_1)

except TypeError:
  print("TypeError")

try:
  choice_b.add_child(choice_b_2)

except TypeError:
  print("TypeError")

I am going to apply your fix later. Nowadays after I finish projects I improve them. I’m going to improve this one because I like this game; I feel like it is to short; there are only 4 possible endings!!! I might share you my code as I improve it and you can help me.
The order is a problem; I should put the traversing at the end.
Anyway now I solved it.

I have this code:
script.py:

# Importing the Dictwriter() function from the csv module:
from csv import DictWriter

# Importing the now() function from the datetime module:
from datetime import datetime

# Imporing the link() method from the os module:
from os import link

# Setting the version number:
version = 0.1

# Setting the score
points = 0

# TreeNode class used to track the story:
class TreeNode:
  # Method that is run when the instance is initialized:
  def __init__(self, story_piece):
    self.story_piece = story_piece
    self.choices = []

  # Used to add story to other stories:
  def add_child(self, node):
    self.choices.append(node)

  # Used to go through the story:
  def traverse(self):
    story_node = self
    print(story_node.story_piece)
    while len(story_node.choices) > 0:
      choice = input("Enter 1 or 2 to continue the story: ")
      if choice in ["1", "2"]:
        choosen_index = int(choice) - 1
        choosen_child = story_node.choices[choosen_index]
        print(choosen_child.story_piece)
        story_node = choosen_child

# The start of the story:
story_root = TreeNode(
"""
You are in a forest clearing. There is a path to the left.
A bear emerges from the trees and roars!
Do you: 
1 ) Roar back!
2 ) Run to the left...
"""
)

# Pieces of story that are needed to go through the story:

# Story piece if the player chooses choice 1:
choice1 = TreeNode(
"""
The bear is startled and runs away.
Do you:
1 ) Shout 'Sorry bear!'.
2 ) Yell 'Hooray!'.
"""
)

# Story piece if the player chooses choice 2:
choice2 = TreeNode(
"""
You come across a clearing full of flowers.The bear follows you and asks 'what gives?'
Do you:
1 ) Gasp 'A talking bear!'.
2 ) Explain that the bear scared you.
"""
)

# Story piece if the player chooses choice 1 and 1:
choice11 = TreeNode(
"""
The bear returns and tells you it's been a rough week. After making peace with a talking bear, he gets a muddy hammer and smashes it to the ground. He laughs.
Do you:
1) Laugh at him.
2) Laugh with him.
"""
)

# Story piece if the player chooses 1 and 2:
choice12 = TreeNode(
"""
The bear returns and tells you that bullying is not okay before leaving you alone in the wilderness. He gives you one more chance to not bully. He puts up a tent and does it badly. 
Do you:
1) Help him.
2) Say that he is so bad at putting tents.
"""
)

# Story piece if the player chooses 2 and 1:
choice21 = TreeNode(
"""
The bear is unamused. After smelling the flowers, it turns around says that he doesn't find it funny.
Do you:
1) Still laugh at him.
2) Say sorry.
"""
)

# Story piece if the player chooses 2 and 2:
choice22 = TreeNode(
"""
The bear understands and apologizes for startling you. Your new friend shows you a path that goes to his home. His home looks like it has been crashed by a giant hammer.
Do you:
1): Say that his house is ugly.
2): Try to help him build his house.
"""
)

# Story piece if the player chooses 1 and 1 and 1:
choice111 = TreeNode(
"""
He angrily yells that you shouldn't try to be mean to him and he leaves you into your house. You lost the game. 
"""
)

# Story piece if the player chooses 1 and 1 and 2:
choice112 = TreeNode(
"""
He says that you have a good sense of humour and helps you find your way out of the wild. You won the game!!!
"""
)

# Story piece if the player chooses choice 1 and 2 and 1:
choice121 = TreeNode(
"""
He says thank you to you and helps you go out of the jungle. You won the game!!!
"""
)

# Story piece if the player chooses choice 1 and 2 and 2:
choice122 = TreeNode(
"""
He says that you should encourage him to do it not make him feel bad. You have to go away from him. You are lost in the wild. You lost the game.
"""
)

# Story piece if the player chooses choice 2 and 1 and 1:
choice211 = TreeNode(
"""
He says not to laugh at him and he leaves you alone. You are lost in the wild. You lost the game.
"""
)

# Story piece if the player chooses choice 2 and 1 and 2:
choice212 = TreeNode(
"""
He forgives you and helps you get out of the jungle. You won the game!!!
"""
) \
points += 1

# Story piece if the player chooses choice 2 and 2 and 1:
choice221 = TreeNode(
"""
He says that you shouldn't say that his house is ugly because it is mean. He makes you leave him. You're lost. You lost the game.
"""
)

# Story piece if the player chooses choice 2 and 2 and 2:
choice222 = TreeNode(
"""
He says that he appreciates your help. You escape the wild. You won the game!!!
"""
)

# Adding the stories to the start of the story and adding some stories to other parts of the story:
story_root.add_child(choice1)
story_root.add_child(choice2)
choice1.add_child(choice11)
choice1.add_child(choice12)
choice2.add_child(choice21)
choice2.add_child(choice22)
choice11.add_child(choice111)
choice11.add_child(choice112)
choice12.add_child(choice121)
choice12.add_child(choice122)
choice21.add_child(choice211)
choice21.add_child(choice212)
choice22.add_child(choice221)
choice22.add_child(choice222)

while True:
  # Introduction to game:
  play = input(
"""
Welcome to Choose Your Own Adventure: Wilderness Escape! You win the game by escaping the wild back to your home. Do you want to play? Enter yes or no. 
"""
  )
  if play == "yes" or play == "no":
    break

# Set if the player plays:
play_again = True

if play == "yes":
    # Asking the player what their name is:
  name = input("What is your name? ")

# Main loop:
while play == "yes" and play_again:

  # Set up a story theme:
  print("Once upon a time...")

  # Go through the story:
  story_root.traverse()

  # Ask player if he or she wants to play again:
  play_again = input(f"You have finished the game. Do you want to play again, {name.title()}? Enter yes or no.").lower()
  # Stops asking if the player enters a valid choice:
  if play_again == "no":
    play_again = False
  points += 1

# Ask if player wants to give feedback:
while True:
  give_feedback = input("Do you want to give feedback? Answer yes or no.").lower()
  if give_feedback == "yes" or give_feedback == "no":
    break

# Ask for feedback:
if give_feedback == "yes":
  feedback = input(f"{name.title()}, How can we improve this game?")

# Saves the feedback into a file:
with open("Feedback.csv", "a", newline = "") as feedback:
  fieldnames = ["Name", "Time","Feedback"]
  writer = DictWriter(feedback, fieldnames = fieldnames)
  writer.writeheader()
  writer.writerow({"Name": name.title(), "Time": datetime.now(11), "Feedback": feedback})

# Finished game message:
print("Horray!!! You have finished the game!!!".upper())

# Tells who made this game:
print("Created by Mario.".upper())

# Tells how to learn Python:
print("If you would like to make a game like me you can take the Learn Python 3 course on Codecademy if you don't know how to code and would like to learn Python: {link(\"Learn Python 3\",\"https://www.codecademy.com/learn/learn-python-3\")}")

# Tells when the player finished the game:
print(f"Finished game at {datetime.now(11)}.")

# Credits:
print(
f"""
Credits:
Produced by Mario.

Idea:
Codecademy and Mario.

Source:
Codecademy.com, Learn Complex Data Structures, Trees, Choose Your Own Adventure: Wilderness Escape.

Version:
{version}.

Thanks to Codecademy!!!

For more information visit {link("Codecademy Forums", "discuss.codecademy.com")} and type in @superpythoncoder1 into a reply, topic, etc to see Mario's profile.
"""
)

# Final goodbye message:
print(
f"""
We hope to see you play this game later.
Goodbye!!!
"""
)

# A line of words that indicate finality:
print("{\"End\".upper()} {\"end \".upper() * 10}")

# Save the number of points into a file:
with open("points.txt","r+") as points:
  points.write(points.readline() + points)

Look specifically at these lines:

story_root = TreeNode(
"""
You are in a forest clearing. There is a path to the left.
A bear emerges from the trees and roars!
Do you: 
1 ) Roar back!
2 ) Run to the left...
"""
)

# Pieces of story that are needed to go through the story:

# Story piece if the player chooses choice 1:
choice1 = TreeNode(
"""
The bear is startled and runs away.
Do you:
1 ) Shout 'Sorry bear!'.
2 ) Yell 'Hooray!'.
"""
)

# Story piece if the player chooses choice 2:
choice2 = TreeNode(
"""
You come across a clearing full of flowers.The bear follows you and asks 'what gives?'
Do you:
1 ) Gasp 'A talking bear!'.
2 ) Explain that the bear scared you.
"""
)

# Story piece if the player chooses choice 1 and 1:
choice11 = TreeNode(
"""
The bear returns and tells you it's been a rough week. After making peace with a talking bear, he gets a muddy hammer and smashes it to the ground. He laughs.
Do you:
1) Laugh at him.
2) Laugh with him.
"""
)

# Story piece if the player chooses 1 and 2:
choice12 = TreeNode(
"""
The bear returns and tells you that bullying is not okay before leaving you alone in the wilderness. He gives you one more chance to not bully. He puts up a tent and does it badly. 
Do you:
1) Help him.
2) Say that he is so bad at putting tents.
"""
)

# Story piece if the player chooses 2 and 1:
choice21 = TreeNode(
"""
The bear is unamused. After smelling the flowers, it turns around says that he doesn't find it funny.
Do you:
1) Still laugh at him.
2) Say sorry.
"""
)

# Story piece if the player chooses 2 and 2:
choice22 = TreeNode(
"""
The bear understands and apologizes for startling you. Your new friend shows you a path that goes to his home. His home looks like it has been crashed by a giant hammer.
Do you:
1): Say that his house is ugly.
2): Try to help him build his house.
"""
)

# Story piece if the player chooses 1 and 1 and 1:
choice111 = TreeNode(
"""
He angrily yells that you shouldn't try to be mean to him and he leaves you into your house. You lost the game. 
"""
)

# Story piece if the player chooses 1 and 1 and 2:
choice112 = TreeNode(
"""
He says that you have a good sense of humour and helps you find your way out of the wild. You won the game!!!
"""
)

# Story piece if the player chooses choice 1 and 2 and 1:
choice121 = TreeNode(
"""
He says thank you to you and helps you go out of the jungle. You won the game!!!
"""
)

# Story piece if the player chooses choice 1 and 2 and 2:
choice122 = TreeNode(
"""
He says that you should encourage him to do it not make him feel bad. You have to go away from him. You are lost in the wild. You lost the game.
"""
)

# Story piece if the player chooses choice 2 and 1 and 1:
choice211 = TreeNode(
"""
He says not to laugh at him and he leaves you alone. You are lost in the wild. You lost the game.
"""
)

# Story piece if the player chooses choice 2 and 1 and 2:
choice212 = TreeNode(
"""
He forgives you and helps you get out of the jungle. You won the game!!!
"""
) \
points += 1

# Story piece if the player chooses choice 2 and 2 and 1:
choice221 = TreeNode(
"""
He says that you shouldn't say that his house is ugly because it is mean. He makes you leave him. You're lost. You lost the game.
"""
)

# Story piece if the player chooses choice 2 and 2 and 2:
choice222 = TreeNode(
"""
He says that he appreciates your help. You escape the wild. You won the game!!!
"""
)

# Adding the stories to the start of the story and adding some stories to other parts of the story:
story_root.add_child(choice1)
story_root.add_child(choice2)
choice1.add_child(choice11)
choice1.add_child(choice12)
choice2.add_child(choice21)
choice2.add_child(choice22)
choice11.add_child(choice111)
choice11.add_child(choice112)
choice12.add_child(choice121)
choice12.add_child(choice122)
choice21.add_child(choice211)
choice21.add_child(choice212)
choice22.add_child(choice221)
choice22.add_child(choice222)

How do I make it so that I set a variable that keep track of if a player loses or wins using True or False(someone, @tgrtim)? I want that because I want to keep track of points.
I think I should change .traverse(). Fact: the code is now 281 lines.

You can look up the valid line joining options here (+ the next couple of paragraphs):
https://docs.python.org/3/reference/lexical_analysis.html#explicit-line-joining

As for True/False it sounds like you answered your own question, lay out how that would work logically, then try to implement it and then test to make sure it works as you expected.

1 Like

So do I change the traverse method? Should I use many ifs and elifs? I’m actually still not sure how to keep track of losing and winning. I have it in the story variables but how do I use conditionals to set True and False when the last story is set depending on losing and winning?
Edit:
I figured it out: you could check if the words that appear in the story variables that indicate losing and winning and set a variable according to that. Is that a efficient/good way to do that?

The problem is there’s more than one possible solution. I think you might be getting a bit caught up in finding the “best” solution when you could already solve this with things you already know.

Efficiency is less of a problem than you might be thinking, you don’t need to optimise every last function (there’s even a well-known term for this- premature optimisation). One of the major selling points of Python is that it is much easier to read and therefore much easier to maintain. Find a working solution; if you really want to make it better than just make sure it can be read and understood easily, that’s all.

I’m going to make it more complicated. Using the variable to keep track of winning and losing and also for every time the player makes a good choice the points increments(1 or another number). I’m also going to put more of the story in; changing 24 TreeNodes(next level 16 possible + 8 previously). It might require about 200 lines of code.

My code is now currently 469 lines long. When I finished the project it was about 95 lines long. Now most of my code was thought up by me. Here it is:

# Importing the Dictwriter() function from the csv module:
from csv import DictWriter

# Importing the now() function from the datetime module:
from datetime import datetime

# Imporing the link() function from the os module:
from os import link

# Importing the timezone function from the pytz module:
from pytz import timezone

# Setting the version number:
version = 0.3

# Setting the score
points = 0

# TreeNode class used to track the story:
class TreeNode:
  # Method that is run when the instance is initialized:
  def __init__(self, story_piece):
    self.story_piece = story_piece
    self.choices = []

  # Used to add story to other stories:
  def add_child(self, node):
    self.choices.append(node)

  # Used to go through the story:
  def traverse(self):
    story_node = self
    print(story_node.story_piece)
    
    while len(story_node.choices) > 0:
      choice = input("Enter 1 or 2 to continue the story: ")
      
      if choice in ["1", "2"]:
        choosen_index = int(choice) - 1
        choosen_child = story_node.choices[choosen_index]
        print(choosen_child.story_piece)
        story_node = choosen_child
        
    game_won = True if "You won!!!" in story_node.story_piece else False

# The start of the story:
story_root = TreeNode(
"""
You are in a forest clearing. There is a path to the left.
A bear emerges from the trees and roars!
Do you: 
1 ) Roar back!
2 ) Run to the left...
"""
)

# Pieces of story that are needed to go through the story:

# Story piece if the player chooses choice 1:
choice1 = TreeNode(
"""
The bear is startled and runs away.
Do you:
1: Shout 'Sorry bear!'.
2: Yell 'Hooray!'.
"""
)

# Story piece if the player chooses choice 2:
choice2 = TreeNode(
"""
You come across a clearing full of flowers.The bear follows you and asks 'what gives?'
Do you:
1: Gasp 'A talking bear!'.
2: Explain that the bear scared you.
"""
)

# Story piece if the player chooses choice 1 and 1:
choice11 = TreeNode(
"""
The bear returns and tells you it's been a rough week. After making peace with a talking bear, he gets a muddy hammer and smashes it to the ground. He laughs.
Do you:
1: Laugh at him.
2: Laugh with him.
"""
)

# Story piece if the player chooses 1 and 2:
choice12 = TreeNode(
"""
The bear returns and tells you that bullying is not okay before leaving you alone in the wilderness. He gives you one more chance to not bully. He puts up a tent and does it badly. 
Do you:
1: Help him.
2: Say that he is so bad at putting tents.
"""
)

# Story piece if the player chooses 2 and 1:
choice21 = TreeNode(
"""
The bear is unamused. After smelling the flowers, it turns around says that he doesn't find it funny.
Do you:
1: Still laugh at him.
2: Say sorry.
"""
)

# Story piece if the player chooses 2 and 2:
choice22 = TreeNode(
"""
The bear understands and apologizes for startling you. Your new friend shows you a path that goes to his home. His home looks like it has been crashed by a giant hammer.
Do you:
1): Say that his house is ugly.
2): Try to help him build his house.
"""
)

# Story piece if the player chooses 1 and 1 and 1:
choice111 = TreeNode(
"""
The bear angrily yells that you shouldn't try to be mean to him and he leaves you. You climb up to a tree and a bird says that he can help you find a way out of the wild. if you be kind.
Do you:
1: Shout at the bird for being so stupid.
2: Thank the bird for the offer.
"""
)

# Story piece if the player chooses 1 and 1 and 2:
choice112 = TreeNode(
"""
The bear says that you have a good sense of humour. He tried to find a path out of the jungle. He goes out to the park and plays with you. He lets you play on the slides.
Do you:
1: Share the slides.
2: Say that he can't go onto the slides because you want to.
"""
)

# Story piece if the player chooses choice 1 and 2 and 1:
choice121 = TreeNode(
"""
The bear says thank you to you and gives you some money.
Do you:
1: Say "You're Welcome"
2: Say "I want all your money."
"""
)

# Story piece if the player chooses choice 1 and 2 and 2:
choice122 = TreeNode(
"""
The bear says that you should encourage him to do it not make him feel bad. You have to go away from him. You jump into a tunnel and try to find food.
Do you:
1: Ask someone if they can give you food.
2: Steal multiple people's food.
"""
)

# Story piece if the player chooses choice 2 and 1 and 1:
choice211 = TreeNode(
"""
The bear says not to laugh at him and he leaves you alone. Do you:
1: Smash his house and steal all of his resorces and make yourself a hiding place.
2: Come back to him and say that you feel sorry.
"""
)

# Story piece if the player chooses choice 2 and 1 and 2:
choice212 = TreeNode(
"""
The bear forgives you and helps you get out of the jungle.He goes to a brown drinking water fountain and lets you drink from it. Your least favourite colour is brown.
Do you:
1: Say that his drinking water fountain is so bad.
2: Say that you appreciate it.
"""
)

# Story piece if the player chooses choice 2 and 2 and 1:
choice221 = TreeNode(
"""
The bear says that you shouldn't say that his house is ugly because it is mean. He makes you leave him. You set off to a journey.
Do you:
1: In the journey you ask for food.
2: In the journey you steal food and treat the same people as a servant.
"""
)

# Story piece if the player chooses choice 2 and 2 and 2:
choice222 = TreeNode(
"""
The bear says that he appreciates your help. He lets you live in his house for the rest of the night. Your room that the bear prepared you didn't have most of the things you liked.
Do you:
1: Complain that you want your room to have most of the things that you like.
2: Say that you are very thankful.
"""
)

# Story piece if the player chooses choice 1 and 1 and 1 and 1:
choice1111 = TreeNode(
"""
The bird leaves you and you are lost in the wild. You lost.
"""
)

# Story piece if the player chooses choice 1 and 1 and 1 and 2:
choice1112 = TreeNode(
"""
\"You're welcome.\" says the bird. The bird takes you out of the wild. You won!!!
"""
)

# Story piece if the player chooses choice 1 and 1 and 2 and 1:

choice1121 = TreeNode(
"""
The bear thanks you and takes you out of the wild. You won!!!
"""

# Story piece if the player chooses choice 1 and 1 and 2 and 2:
choice1122 = TreeNode(
"""
The bear is in rage. He says that you have to share. He leaves you and you are lost in the wild. You lost.
"""
)

# Story piece if the player chooses choice 1 and 2 and 1 and 1 and 1:
choice1211 = TreeNode(
"""
The bear takes you out of the wild. You won!!!
"""
)

# Story piece if the player chooses choice 1 and 2 and 1 and 2:
choice 1212 = TreeNode(
"""
The bear shouts to you not to be greedy and he leaves you alone. You are lost in the wild. You lost.
"""
)

# Story piece if the player chooses choice 1 and 2 and 2 and 1:
choice1221 = TreeNode(
"""
You ask some people and they say no but one gives you some food and they take you out of the wild. You won!!!
"""
)

# Story piece if the player chooses choice 1 and 2 and 2 and 2:
choice1222 = TreeNode(
"""
The first person you steal food from catches you and takes the food back. The person rebukes that you get out of the house. You are lost in the wild. You lost.
"""
)

# Story piece if the player chooses choice 2 and 1 and 1 and 1:
choice2111 = TreeNode(
"""
He finds out that his house is destroyed and sends you 1000 kilometres away from his house in another jungle. You are lost in the wild. You lost.
"""
)

# Story piece if the player chooses choice 2 and 1 and 1 and 2:
choice2112 = TreeNode(
"""
One of the people you asked gave you food and helps you find a way out of the wild. You won!!!
"""
)

# Story piece if the player chooses choice 2 and 1 and 2 and 1:
choice2121 = TreeNode(
"""
The bear shouts in sadness that you shouldn't say that someone's things are bad and he leaves you in the wild. You lost.
"""
)

# Story piece if the player chooses choice 2 and 1 and 2 and 2:
choice2122 = TreeNode(
"""The bear thanks you for saying that and helps you find a way out of the wild. You won!!!
"""
)

# Story piece if the player chooses choice 2 and 2 and 1 and 1:
choice2211 = TreeNode(
"""
In your journey, you ask some people but they didn't want to. One person says yes and gives you food. He helps you find your way out of the wild. You won!!!
"""
)

# Story piece if the player chooses choice 2 and 2 and 1 and 2:
choice2212 = TreeNode(
"""
You asked some people and they didn't want to. One person says they do want to and gives you food. You treat them like a servant and they said they wanted their food back.You are lost in the wild. You lost. 
"""
)

# Story piece if the player choses choice 2 and 2 and 2 and 1:
choice2221 = TreeNode(
"""
The bear becomes angry and leaves you alone and you are lost in the wild. You lost.
"""
)

# Story piece if the player choses choice 2 and 2 and 2 and 2:
choice2222 = TreeNode(
"""
The bear thanks you and helps you get out of the wild. You lost.
"""
)

# Adding the stories to the start of the story and adding some stories to other parts of the story:
story_root.add_child(choice1)
story_root.add_child(choice2)
choice1.add_child(choice11)
choice1.add_child(choice12)
choice2.add_child(choice21)
choice2.add_child(choice22)
choice11.add_child(choice111)
choice11.add_child(choice112)
choice12.add_child(choice121)
choice12.add_child(choice122)
choice21.add_child(choice211)
choice21.add_child(choice212)
choice22.add_child(choice221)
choice22.add_child(choice222)
choice111.add_child(choice1111)
choice111.add_child(choice1112)
choice112.add_child(choice1121)
choice112.add_child(choice1122)
choice121.add_child(choice1211)
choice121.add_child(choice1212)
choice122.add_child(choice1221)
choice122.add_child(choice1222)
choice211.add_child(choice2111)
choice211.add_child(choice2112)
choice212.add_child(choice2121)
choice212.add_child(choice2122)
choice221.add_child(choice2211)
choice221.add_child(choice2212)
choice222.add_child(choice2221)
choice222.add_child(choice2222)

while True:
  # Introduction to game:
  play = input(
"""
Welcome to Choose Your Own Adventure: Wilderness Escape! You win the game by escaping the wild back to your home. Do you want to play? Enter yes or no: """
  )
  
  if play == "yes" or play == "no":
    break

# Set if the player plays:
play_again = True

# Ask the player for his/her name:
name = input("What is your name? ")

# Main loop:
while play == "yes" and play_again:
  # Set up a story theme:
  print("Once upon a time...")

  # Go through the story:
  story_root.traverse()

  # Ask player if he or she wants to play again:
  while True:
    play_again = input(f"You have finished the game. Do you want to play again, {name.title()}? Enter yes or no: ").lower()

    if play_again == "yes" or play_again == "no":
      break

  # Stops asking if the player enters a valid choice:
  if play_again == "no":
    play_again = False

# Ask if player wants to give feedback:
while True:
  give_feedback = input("Do you want to give feedback? Answer yes or no: ").lower()

  if give_feedback == "yes" or give_feedback == "no":
    break

# Ask for feedback:
if give_feedback == "yes":
  feedback = input(f"{name.title()}, How can we improve this game?" )

# Saves the feedback into a file:
with open("Feedback.csv", "a", newline = "") as feedback:
  fieldnames = ["Name", "Time","Feedback"]
  writer = DictWriter(feedback, fieldnames = fieldnames)
  writer.writeheader()
  writer.writerow({"Name": name.title(), "Time": datetime.now(timezone("Australia/NSW")), "Feedback": feedback})

# Finished game message:
print("Horray!!! You have finished the game!!!".upper())

# Save the number of points into a file:
with open("Scores.csv", "a+") as scores:
  fieldnames = ["Name", "Time", "Score"]
  writer = DictWriter(scores, fieldnames = fieldnames)
  writer.writeheader()
  writer.writerow({"Name": name.title(), "Time": datetime.now(timezone("Australia/NSW"), "Score": points)})

  # Save the program's highest 10 scores:
  with open("Highest Scores.csv", "a+", newline = "") as high_scores:
    fieldnames = ["Name", "Time", "High Score"]
    writer = DictWriter(high_scores, fieldnames = fieldnames)
    largests = []

    for item in scores.readlines():
      largests.append(item)

      if len(largests) > 10:
        # Removes the most lowest score:
        del largests[largests.index(sorted(largests)[0])]

# Find the player's rank:
player_rank = ranks.index(f"{points} points.")

# Gives a rank message:
print(f"You've come number {player_rank}!!! Try to aim up to the 10 highest point scores here:")

# Puts the ten highest scores on the screen:
with open("Highest Point Scores.txt") as high_scores:
  for item in high_scores.readlines():
    print(item)

# Tells who made this game:
print("Created by Mario.".upper())

# Tells how to learn Python:
print("If you would like to make a game like me you can take the Learn Python 3 course on Codecademy if you don't know how to code and would like to learn Python: {link(\"Learn Python 3\",\"https://www.codecademy.com/learn/learn-python-3\")}")

# Tells when the player finished the game:
print(f"Finished game at {datetime.now(11)}.")

# Credits:
print(
f"""
Credits:
Produced by Mario.

Idea:
Codecademy and Mario.

Source:
Codecademy.com, Learn Complex Data Structures, Trees, Choose Your Own Adventure: Wilderness Escape.

Version:
{version}.

Thanks to Codecademy!!!

For more information visit {link("Codecademy Forums", "discuss.codecademy.com")} and type in @superpythoncoder1 into a reply, topic, etc to see Mario's profile.
Here is my profile link:
{link("@superpythoncoder1 profile", "https://discuss.codecademy.com/u/superpythoncoder1/summary")}
"""
)

# Final goodbye message:
print(
f"""
We hope to see you play this game later.
Goodbye {name}!!!
"""
)

# A line of words that indicate finality:
print(f"###{'end'.upper() * 10}###")

I’m still going to improve it. Just 5 hours ago it was 316 lines long.