Why a NameError Isn't There

Look at this piece of code:

choice212 = TreeNode(
"""
The bear forgives you and helps you get out of the jungle. He goes to a {least_favourite_colour} drinking water fountain and lets you drink from it.
 
Do you:
1: Say that his drinking water fountain is so bad.
2: Say that you appreciate it.
"""
)

least_favourite_colour is not defined yet in the code above(this is line 284 -292) but the variable choice121 if it is used after least_favourite_colour is defined(this is line 499(actually inside the traverse() method which is before least_favourite_colour is defined). It wouldn’t show an error. Is this correct?
To show you the big picture of the code:

# Importing the Dictwriter() function from the csv module:
from csv import DictWriter
 
# Importing the datetime class from the datetime module
from datetime import datetime
 
# Importing 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 = 2.0
# 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):
    # Make points available in the class:
    global points
    
    story_node = self
    print(story_node.story_piece)
    
    while len(story_node.choices) > 0:
      choice = input("Enter 1 or 2 to continue the story. \n")
      
      if choice in ["1", "2"]:
        choosen_index = int(choice) - 1
        choosen_child = story_node.choices[choosen_index]
        print(choosen_child.story_piece)
 
        # Used to reduce typing because I don't type a string containing a number; I just type the number:
        choice = int(choice)
 
        # Used to figure out if the player earns points or loses points:
        if story_node == story_root and choice == 2:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice1 and choice == 1:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice2 and choice == 2:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice11 and choice == 2:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice12 and choice == 1:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice21 and choice == 2:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice22 and choice == 2:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice111 and choice == 2:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice112 and choice == 1:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice121 and choice == 1:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice122 and choice == 1:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice211 and choice == 2:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice212 and choice == 2:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice221 and choice == 1:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice222 and choice == 2:
          points += 1
 
        else:
          points -= 1
          
        
        story_node = choosen_child
        
    # Manage game wins and loses:
    game_won = True if "You won!!!" in story_node.story_piece else False
    
    if game_won:
      points += 1
 
    else:
      points -= 1
 
# 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 and 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 are 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 and not make him feel bad. You have to get away from him. You jump into a tunnel and try to find food.
 
Do you:
1: Ask someone if he/she 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 resources 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 {least_favourite_colour} drinking water fountain and lets you drink from it.
 
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 on a journey.
 
Do you:
1: On the journey you ask for food.
2: On 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: Complaint 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:
choice1212 = 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 you to 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 helped 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 don'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)
 
# Make starting clearer:
print(f"{(('start '.upper()) * 11) + 'start'.upper()} \n")
 
while True:
  # Introduction to game:
  play = input(
  """
Welcome to Choose Your Own Adventure: Wilderness Escape!
You win the game by escaping the wild.
Do you want to play? 
Enter yes or no. \n
  """
  ).lower()
  
  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? \n").title()
 
# Main loop:
while play == "yes" and play_again:
  accepted_least_favourite_colours = ("blue", "red", "green", "black", "gold", "orange", "purple", "pink", "red", "indigo", "silver", "bronze", "violet", "brown", "white")

  while True:
    least_favourite_colour = input("What is your favourite colour? Please only enter one word(don't enter light blue; just enter blue). \n").lower()

    if least_favourite_colour in accepted_least_favourite_colours:
      break

    print("Please enter a valid colour.")

  # 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 at {datetime.now(timezone('Australia/NSW'))}. Do you want to play again, {name}? Enter yes or no. \n
    """
).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
 
if play:
  # Finished game message:
  print("Hooray!!! You have finished the game!!!")
 
  # Tells when the player finished the game:
  print(f"You finished the game at {datetime.now(timezone('Australia/NSW'))}.")
 
  # Ask if player wants to give feedback:
  while True:
    give_feedback = input(
    """
Do you want to give feedback? Answer yes or no. \n
    """
    ).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? \n
    """
    )
 
    # 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, "Time": datetime.now(timezone("Australia/NSW")), "Feedback": feedback})
 
# 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, "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.reader():
      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(points)
 
  with open("Highest Scores.csv") as high_scores:
    reader = high_scores.reader()
    for item in reader:
      if points in item[2]:
        # Gives a rank message saying that the player was in the top ten scores:
        print(f"You've come number {player_rank}!!! You in the top ten scores here")
      else:
        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.csv") as high_scores:
    for item in reader:
      print(item[2])

…(more code)
.

I haven’t run the code, but it appears that you aren’t using an f-string, so the first code snippet will print "…{least_favourite_color}…", instead of actually printing the contents of that variable; the code is treating the {least_favourite_color} as just part of the string, and not a reference to a variable. Sorry if I’ve missed the point of the question!

Oh yes! I forgot to make it as an f string.

1 Like

Now there is an error(which I don’t want). But now I have to move the choice212 after where least_favourite_colour is defined. I would rather move all the choice variables together so it makes more sense. I am going to fix the error, maybe meanwhile you can give me an position suggestion(s).

You could always move the least_favourite_colour variable above the choice variables…

That’s a good idea because I don’t want to move all the choice variables after defining least_favourite_colour variables because of I like that order. But there are some bad things(relating to position) if move the colour variable. If I move it it will go before even starting the game. So maybe I should just move it after defining the colour variable.

1 Like

It’s actually getting more complex because I copied and pasted and I have to indent hundreds of lines. I accidently deleted hundreds of very important lines of code and I can’t get it back because I used copy and paste on another piece of code. Sometimes not automatically saving is better than if it is because sometimes I delete big amounts of code even sometimes hundreds(about 300) but luckily I have a google document that I can copy to restore back my code.
I just going not move the choice variables and move others.

I like this code:

# Importing the Dictwriter() function from the csv module:
from csv import DictWriter
 
# Importing the datetime class from the datetime module
from datetime import datetime
 
# Importing 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 = 2.0
# 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):
    # Make points available in the class:
    global points
    
    story_node = self
    print(story_node.story_piece)
    
    while len(story_node.choices) > 0:
      choice = input("Enter 1 or 2 to continue the story. \n")
      
      if choice in ["1", "2"]:
        choosen_index = int(choice) - 1
        choosen_child = story_node.choices[choosen_index]
        print(choosen_child.story_piece)
 
        # Used to reduce typing because I don't type a string containing a number; I just type the number:
        choice = int(choice)
 
        # Used to figure out if the player earns points or loses points:
        if story_node == story_root and choice == 2:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice1 and choice == 1:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice2 and choice == 2:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice11 and choice == 2:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice12 and choice == 1:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice21 and choice == 2:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice22 and choice == 2:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice111 and choice == 2:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice112 and choice == 1:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice121 and choice == 1:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice122 and choice == 1:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice211 and choice == 2:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice212 and choice == 2:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice221 and choice == 1:
          points += 1
 
        else:
          points -= 1
 
        if story_node == choice222 and choice == 2:
          points += 1
 
        else:
          points -= 1
          
        
        story_node = choosen_child
        
    # Manage game wins and loses:
    game_won = True if "You won!!!" in story_node.story_piece else False
    
    if game_won:
      points += 1
 
    else:
      points -= 1
 
# Make starting clearer:
print(f"{(('start '.upper()) * 11) + 'start'.upper()} \n")

while True:
  # Introduction to game:
  play = input(
  """
Welcome to Choose Your Own Adventure: Wilderness Escape!
You win the game by escaping the wild.
Do you want to play? 
Enter yes or no. \n
  """
  ).lower()
  
  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? \n").title()
 
# Main loop:
while play == "yes" and play_again:
  accepted_least_favourite_colours = ("blue", "red", "green", "black", "gold", "orange", "purple", "pink", "red", "indigo", "silver", "bronze", "violet", "brown", "white", "aqua", "turquoise", "clear")
 
  while True:
    least_favourite_colour = input("What is your favourite colour? Please only enter one word(don't enter light blue; just enter blue). \n").lower()
 
    if least_favourite_colour in accepted_least_favourite_colours:
      break
 
    print("Please enter a valid colour.")
# 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 and 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 are 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 and not make him feel bad. You have to get away from him. You jump into a tunnel and try to find food.
 
Do you:
1: Ask someone if he/she 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 resources 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 {least_favourite_colour} drinking water fountain and lets you drink from it.
 
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 on a journey.
 
Do you:
1: On the journey you ask for food.
2: On 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: Complaint 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:
choice1212 = 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 you to 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 helped 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 don'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 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 at {datetime.now(timezone('Australia/NSW'))}. Do you want to play again, {name}? Enter yes or no. \n
    """
).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
 
if play:
  # Finished game message:
  print("Hooray!!! You have finished the game!!!")
 
  # Tells when the player finished the game:
  print(f"You finished the game at {datetime.now(timezone('Australia/NSW'))}.")
 
  # Ask if player wants to give feedback:
  while True:
    give_feedback = input(
    """
Do you want to give feedback? Answer yes or no. \n
    """
    ).lower()
 
    if give_feedback == "yes" or give_feedback == "no":
      break
 
  if give_feedback == "yes":
    # Ask for feedback:
    feedback = input(
    f"""
{name.title()}, How can we improve this game? \n
    """
    )
 
    # 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, "Time": datetime.now(timezone("Australia/NSW")), "Feedback": feedback})
 
# 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, "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.reader():
      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(points)
 
  with open("Highest Scores.csv") as high_scores:
    reader = high_scores.reader()
    for item in reader:
      if points in item[2]:
        # Gives a rank message saying that the player was in the top ten scores:
        print(f"You've come number {player_rank}!!! You in the top ten scores here")
      else:
        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.csv") as high_scores:
    for item in reader:
      print(item[2])

…(more code)
What if the code asked what timezone you are in? That could be useful because not everyone is in the same timezone as me.

Yes; you could store that to a variable and pass that into the function instead of what you currently have!

The problem is that I would have to convert that to a pytz recognized timezone and it would take lots of lines. I should do it later.