Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. #
  3. # A n i m a l . p y
  4. #
  5. # Copyright 2001 by Chris Meyers.
  6. #
  7. import string
  8.  
  9. class node :
  10. "Node objects have a question, and left and right pointer to other nodes"
  11. def __init__ (self, question, left=None, right=None) :
  12. self.question = question
  13. self.left = left
  14. self.right = right
  15.  
  16. def yes (ques) :
  17. "Force the user to answer "yes" or "no" or something similar. Yes returns true"
  18. while 1 :
  19. ans = raw_input (ques)
  20. ans = string.lower(ans[0:1])
  21. if ans == 'y' : return 1
  22. else : return 0
  23.  
  24. knowledge = node("bird")
  25.  
  26. def main () :
  27. "Guess the animal. Add a new node for a wrong guess."
  28.  
  29. while 1 :
  30. print
  31. if not yes("Are you thinking of an animal? ") : break
  32. p = knowledge
  33. while p.left != None :
  34. if yes(p.question+"? ") : p = p.right
  35. else : p = p.left
  36.  
  37. if yes("Is it a " + p.question + "? ") : continue
  38. animal = raw_input ("What is the animals name? ")
  39. question = raw_input ("What question would distinguish a %s from a %s? "
  40. % (animal,p.question))
  41. p.left = node(p.question)
  42. p.right = node(animal)
  43. p.question = question
  44.  
  45. if not yes ("If the animal were %s the answer would be? " % animal) :
  46. (p.right, p.left) = (p.left, p.right)
  47.  
  48. if __name__ == "__main__" : main ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement