import markovify f = open("mobydickorwhale01melvuoft_djvu.txt") file_contents = f.read() # Build the model. text_model = markovify.Text(file_contents) story = { "start": { "message": "You are standing in front of a house. Do you (a) go in, or (b) walk away.", "a": "enter", "b": "boring end" }, "enter": { "message": "Everything is dark. Do you (a) look for a lightswitch, or (b) light a match.", "a": "room", "b": "boom end" }, "room": { "message": "You're standing in a large foyer. Do you (a) go up the stairs, (b) leave.", "a": "win", "b": "start" }, "win": { "message": "You walk up the steps to the top floor and find a pot of gold. You win!", "exit": True }, "boom end": { "message": "There must have been a gas leak. When you light the match there is a big explosion. Game over.", "exit": True }, "boring end": { "message": "OK. Game over.", "exit": True }, } game_state_name = "start" while True: game_state = story[game_state_name] sentence = text_model.make_sentence() print(sentence) print(game_state["message"]) if "exit" in game_state: exit() choice = input("> ") if choice.lower() == "a": game_state_name = game_state["a"] else: game_state_name = game_state["b"]