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)whilelen(story_node.choices)!=0:
answer =int(input("Enter 0 or 1 to continue the story: "))if answer ==0or 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")
current_node.add_choices(choice_a, choice_b)
current_node = choice_a
choice_a = TreeNode("You travel back to 1980 and get stuck in an arcade.\n")
choice_b = TreeNode("You travel forward to 3000 and see that not much has changed but people live underwater. \nDo you: \n 0 ) Swim into the city.\n 1 ) Stay in the phone booth.\n")
current_node.add_choices(choice_a, choice_b)
current_node = choice_b
choice_a = TreeNode("You swim into the city and meet some merpeople.\n Do you: \n 0 ) Try to talk to them. \n 1 ) Swim away in fear.\n")
choice_b = TreeNode("You stay in the phone booth and get sent back home.\n")
current_node.add_choices(choice_a, choice_b)
current_node = choice_a
choice_a = TreeNode("The merpeople welcome you to their city.")
choice_b = TreeNode("You swim out of the city and become lost.")
current_node.add_choices(choice_a, choice_b)
play_again ="y"while play_again =="y":
story_root.tell_story()
play_again =input("Would you like to play again [y]/[n]?: ").lower()
Login
Accessing this project requires a login, please enter your credentials below!