Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. #!python3
  2.  
  3. '''
  4. This is a guess the animal game, where the
  5. conputer asks questions trying to guess at an
  6. animal the user thought of. If the computer
  7. does not know an animal, it asks the user to
  8. provide a question to differentiate from
  9. already known animals and so learn more.
  10. It's implemented by using a binary tree where
  11. the branches are the questions and the leaves
  12. are animals, and it's traversed recursively.
  13. It saves what it learns in a text file.
  14. '''
  15.  
  16. class Node():
  17. def __init__(self):
  18. pass
  19. def Save(self, file):
  20. pass
  21. def Load(self, file):
  22. line = file.readline()
  23. line = line.replace("\n", "")\
  24. .replace("\r", "")
  25. if line[:1] == "Q":
  26. return Question(line[3:], \
  27. Node.Load(self, file), \
  28. Node.Load(self, file))
  29. elif line[:1] == "A":
  30. return Animal(line[3:])
  31.  
  32. class Question(Node):
  33. def __init__(self, question, yesNode, noNode):
  34. self.question = question
  35. self.yes = yesNode
  36. self.no = noNode
  37. def Save(self, file):
  38. file.write("Q: " + self.question + "\n")
  39. self.yes.Save(file)
  40. self.no.Save(file)
  41.  
  42. class Animal(Node):
  43. def __init__(self, animalName):
  44. self.animal = animalName
  45. def Save(self, file):
  46. file.write("A: " + self.animal + "\n")
  47.  
  48. def UserSaidYes(text):
  49. while True:
  50. response = input(text + " (y/n) ")
  51. if response in ["Y", "y"]:
  52. return True
  53. elif response in ["N", "n"]:
  54. return False
  55. else:
  56. print("Sorry, didn't get that")
  57.  
  58. def Article(noun):
  59. if noun[:1].lower() in ["a", "e", "i", "o", "u"]:
  60. return "an"
  61. else:
  62. return "a"
  63.  
  64. def Ask(node):
  65. if type(node) == Question: # branch node
  66. if UserSaidYes(node.question):
  67. result = Ask(node.yes)
  68. if type(result) is Question:
  69. node.yes = result
  70. return False
  71. else:
  72. return result
  73. else:
  74. result = Ask(node.no)
  75. if type(result) is Question:
  76. node.no = result
  77. return False
  78. else:
  79. return result
  80.  
  81. else: # it's an animal - leaf node
  82. if UserSaidYes("Is the animal you thought " + Article(node.animal) + " " + node.animal):
  83. print("I knew it! I win!\r\n")
  84. return True
  85. else:
  86. print("Oh, you win!\r\n")
  87. newAnimal = input("What was the animal you thought? ")
  88. newQuestion = input("Can you tell me a question to differenciate between " \
  89. + Article(newAnimal) + " " + newAnimal \
  90. + " and " + Article(node.animal) + " " + node.animal + "? ")
  91. if newQuestion[-1:] != "?":
  92. newQuestion += "?"
  93. if UserSaidYes("For " + Article(newAnimal) + " " + newAnimal \
  94. + " what would you reply to that question?"):
  95. return Question(newQuestion, Animal(newAnimal), Animal(node.animal))
  96. else:
  97. return Question(newQuestion, Animal(node.animal), Animal(newAnimal))
  98.  
  99. def SampleData():
  100. return Question("Does it live in the sea?", Animal("Dolphin"), Animal("Lion"))
  101.  
  102. def Load():
  103. # Try to load previous dataset value from file:
  104. try:
  105. with open('animals.txt') as f:
  106. return Node().Load(f)
  107. except IOError:
  108. return SampleData()
  109. except:
  110. return SampleData()
  111.  
  112. def Save(data):
  113. # Save the new dataset value to a file:
  114. with open('animals.txt', 'w') as f:
  115. data.Save(f)
  116.  
  117. def main():
  118. animals = Load()
  119. me = you = 0
  120. print("PyAnimals!\r\n")
  121. while True:
  122. print("Choose an animal and I'll try to guess it.\r\n")
  123. if Ask(animals):
  124. me += 1
  125. else:
  126. Save(animals)
  127. you += 1
  128. print("You: " + str(you) + " Me: " + str(me))
  129. if UserSaidYes("\r\nWant to play again?"):
  130. continue
  131. else:
  132. break
  133.  
  134. if __name__ == '__main__':
  135. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement