Guest User

Untitled

a guest
Feb 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. class Tree:
  2.     def __init__(self, cargo, left=None, right=None):
  3.         self.cargo = cargo
  4.         self.left  = left
  5.         self.right = right
  6.  
  7.     def __str__(self):
  8.         return str(self.cargo)
  9.  
  10. def yes(ques):
  11.     ans = raw_input(ques).lower()
  12.     return ans[0] == 'y'
  13.  
  14. def animal():
  15.     # start with a singleton
  16.     root = Tree("bird")
  17.  
  18.     # loop until the user quits
  19.     while True:
  20.         print
  21.         if not yes("Are you thinking of an animal? "): break
  22.  
  23.         # walk the tree
  24.         tree = root
  25.         while tree.left != None:
  26.             prompt = tree.cargo + "? "
  27.             if yes(prompt):
  28.                 tree = tree.right
  29.             else:
  30.                 tree = tree.left
  31.  
  32.         # make a guess
  33.         guess = tree.cargo
  34.         prompt = "Is it a " + guess + "? "
  35.         if yes(prompt):
  36.             print "I rule!"
  37.             continue
  38.  
  39.         # get new information
  40.         prompt  = "What is the animal's name? "
  41.         animal  = raw_input(prompt)
  42.         prompt  = "What question would distinguish a %s from a %s? "
  43.         question = raw_input(prompt % (animal, guess))
  44.  
  45.         # add new information to the tree
  46.         tree.cargo = question
  47.         prompt = "If the animal were %s the answer would be? "
  48.         if yes(prompt % animal):
  49.             tree.left = Tree(guess)
  50.             tree.right = Tree(animal)
  51.         else:
  52.             tree.left = Tree(animal)
  53.             tree.right = Tree(guess)
Add Comment
Please, Sign In to add comment