# CSE 143, Winter 2010, Roy McElmurry # Homework 7: 20 Questions from questiontree import * # 'Constants' for messages LOAD_MESSAGE = "Shall I recall our previous games? " PLAY_AGAIN_MESSAGE = "Challenge me again? " SAVE_MESSAGE = "Shall I remember these games? " LOAD_MESSAGE = "Shall I recall our previous games? " SAVE_LOAD_FILENAME_MESSAGE = "What is the file name? " BANNER_MESSAGE = "Think of an item, and I will guess it. " # Loads the previous game data based on user input def load(): answer = input(LOAD_MESSAGE).lower() if (answer.startswith("y")): file_name = input(SAVE_LOAD_FILENAME_MESSAGE) try: tree.load(file_name) except IOError: print("Error: There was an error opening the provided file.") # Saves the current game based on user input def save(): if (input(SAVE_MESSAGE).lower().startswith("y")): file_name = input(SAVE_LOAD_FILENAME_MESSAGE) try: tree.save(file_name) except IOError: print("Error: There was an error opening the provided file.") # Main # Manages game play according to user input print("Welcome to the game of 20 Questions!") tree = QuestionTree() load() print("\n" + BANNER_MESSAGE) print() tree.play() while (input(PLAY_AGAIN_MESSAGE).lower().startswith("y")) : print() tree.play() print("Games played: " + str(tree.total_games()) + "\nI won " + str(tree.games_won())) save()