This is an incomplete tree diagram I made to classify this list of animals:
horse, cow, sheep, pig, dog, cat, lion, tiger, whale, dolphin, seal, penguin, ostrich, sparrow, spider, ant, bee, wasp, termite, octopus, squid
I havent finished putting in dog, tiger, lion, etc. just because the application required me to buy some subscription for more shapes and i wasn't gonna do that, but that doesn't matter cos I can visualise the rest. My question is; In python code, how can I make a program that asks the user yes/no questions continuously until it can make out what animal it is out of the list. I can obviously do this with lots of IF statements, or with OOP and using attributes, however both solutions require me to ask EVERY single question, which would amount to quite a lot of lines of code, and it would be quite ugly. How do I make it, for example, so that if the user says that their animal is aquatic, it no longer asks any of the questions that don't apply to the animal. For example:
If I pick wasp, and I answer yes to the question "Is your animal a land animal?", then no to "Is your animal a mammal?", then yes to it being a carnivore and being able to fly, how do i make it so the program will only branch to those questions? Basically, how do i code a tree diagram that follows the user's inputs? (I dont need any GUI)
You could define the tree with a classical Node
class:
class Node:
def __init__(self, name, *children):
self.name = name
self.children = children
tree = Node("an animal",
Node("aquatic",
Node("a cephalapod",
Node("having 8 tentacles", Node("an octopus")),
Node("having 10 tentacles", Node("a squid"))
), Node("a whale",
Node("a baleen", Node("a baleen")),
Node("not a baleen", Node("a dolphin"))
)
), Node("a land animal",
Node("a mammal",
Node("a bird",
Node("able to fly", Node("a sparrow")),
Node("not able to fly",
Node("a piscivore", Node("a penguin")),
Node("not a piscivore", Node("an ostrich"))
)
), Node("not a bird",
Node("a carnivore", Node("a lion")),
Node("a herbivore",
Node("having leather", Node("a bovine")),
Node("not having leather",
Node("having wool", Node("a sheep")),
Node("not having wool", Node("a pig"))
)
)
)
), Node("an insect",
Node("a carnivore",
Node("able to fly", Node("a wasp")),
Node("not able to fly",
Node("having 6 legs", Node("an ant")),
Node("having 8 legs", Node("a spider"))
)
), Node("a herbivore",
Node("able to fly", Node("a bee")),
Node("not able to fly", Node("a termite"))
)
)
)
)
Then the main code can continue with the question loop:
node = tree
while node.children:
answer = "n"
for child in node.children[:-1]:
print("Is it {}? (y/n)".format(child.name))
answer = input()
if answer.lower() == "y":
break
if answer.lower() != "y":
child = node.children[-1]
node = child
print("It is {}".format(node.name))
Although your tree is binary, this code foresees the possibility to have more than 2 children. When answering no to the question that would select the first child, it will then ask the question that would lead to the second child, ...etc, until only one other child is left as possibility: it will not ask the corresponding question, since that is the only option left.