CHAPTER 8

The Learning Tree 🌳

How does a computer play "20 Questions"? It uses a Decision Tree.

It asks a question, splits the data, and asks again. Let's help Robo guess the animal!

🌿 Branching Logic

We use Nested Ifs (an `if` inside an `if`).

if swims == "yes":
  if quacks == "yes":
    print("Duck")

Swims?
↙
Yes
Duck
β†˜
No
Cat

GUESS THE ANIMAL 🐾

Step 1: The Features

Our mystery animal swims but does NOT quack. Set the variables.

swims = "yes"
quacks = "no"
Waiting for you to run code...

Step 2: The First Branch

Write the outer if to check if it swims.

if swims == "yes":
    # We will put more logic here
    print("It likes water")
else:
    print("It is a Cat")
Locked πŸ”’

Step 3: The Nested Branch

Inside the first `if`, check if it quacks. If not, it's a Fish!

if swims == "yes":
    if quacks == "yes":
        print("Duck")
    else:
        print("Fish")
Locked πŸ”’
🐱

Logic Check πŸ“

1. Why is it called a "Decision Tree"?

⌨️ LOGIC LAB
Waiting...