Bird
Raised Fist0
Agentic AIml~10 mins

Branching and conditional logic in Agentic AI - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the input number is positive.

Agentic AI
if number [1] 0:
    print("Positive number")
Drag options to blanks, or click blank then click option'
A>
B!=
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' will check for negative numbers.
Using '==' checks only if the number equals zero.
2fill in blank
medium

Complete the code to print 'Even' if the number is divisible by 2.

Agentic AI
if number [1] 2 == 0:
    print("Even")
Drag options to blanks, or click blank then click option'
A//
B%
C+
D**
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '**' operators will cause errors or wrong logic.
Using '//' gives the quotient, not the remainder.
3fill in blank
hard

Fix the error in the condition to check if age is between 18 and 30.

Agentic AI
if 18 [1] age [2] 30:
    print("Eligible")
Drag options to blanks, or click blank then click option'
A<
B>
C<=
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' reverses the logic.
Using '>=' or '<=' changes the range to include boundaries.
4fill in blank
hard

Fill both blanks to create a dictionary of words with length greater than 3.

Agentic AI
{word: len(word) for word in words if len(word) [1] 3 and word [2] 'test'}
Drag options to blanks, or click blank then click option'
A>
B==
C!=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '==' in the first blank changes the length condition.
Using '==' in the second blank includes 'test' instead of excluding it.
5fill in blank
hard

Fill all three blanks to filter a dictionary with keys starting with 'a' and values greater than 10.

Agentic AI
{k: v for k, v in data.items() if k[1]'a') and v [2] 10 and isinstance(v, [3])}
Drag options to blanks, or click blank then click option'
A.startswith(
B>
Cint
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '.startswith(' causes errors.
Using '==' instead of '>' changes the value condition.
Using wrong type instead of 'int' causes type check failure.

Practice

(1/5)
1. What does branching in agentic AI primarily help with?
easy
A. Speeding up calculations without decisions
B. Storing large amounts of data
C. Visualizing data in graphs
D. Choosing actions based on conditions

Solution

  1. Step 1: Understand the purpose of branching

    Branching allows AI to make choices depending on different conditions it checks.
  2. Step 2: Match branching to its main use

    Choosing actions based on conditions is exactly what branching does in AI decision-making.
  3. Final Answer:

    Choosing actions based on conditions -> Option D
  4. Quick Check:

    Branching = Choosing actions [OK]
Hint: Branching means picking actions by checking conditions [OK]
Common Mistakes:
  • Confusing branching with data storage
  • Thinking branching speeds up calculations only
  • Mixing branching with visualization tasks
2. Which of the following is the correct syntax to start a conditional branch in agentic AI code?
easy
A. if condition:
B. when condition then
C. check condition {}
D. condition if:

Solution

  1. Step 1: Recall correct conditional syntax

    In agentic AI (like Python), conditions start with 'if' followed by the condition and a colon.
  2. Step 2: Compare options to syntax rules

    Only 'if condition:' matches the correct syntax for starting a branch.
  3. Final Answer:

    if condition: -> Option A
  4. Quick Check:

    Correct syntax starts with 'if' and colon [OK]
Hint: Remember: 'if' + condition + colon starts a branch [OK]
Common Mistakes:
  • Using 'when' instead of 'if'
  • Missing colon after condition
  • Placing condition after 'if' incorrectly
3. What will this agentic AI code print?
score = 75
if score >= 90:
    print('Excellent')
elif score >= 60:
    print('Good')
else:
    print('Needs Improvement')
medium
A. Excellent
B. Good
C. Needs Improvement
D. No output

Solution

  1. Step 1: Check the score against conditions

    Score is 75, which is not >= 90, so first condition fails.
  2. Step 2: Check elif condition

    75 is >= 60, so 'Good' will be printed.
  3. Final Answer:

    Good -> Option B
  4. Quick Check:

    75 >= 60 triggers 'Good' [OK]
Hint: Check conditions top to bottom, first true runs [OK]
Common Mistakes:
  • Choosing 'Excellent' because 75 is high
  • Ignoring elif and jumping to else
  • Thinking no output if first condition fails
4. Identify the error in this agentic AI branching code:
if temperature > 30
    print('Hot')
elif temperature > 20:
    print('Warm')
medium
A. Missing colon after first if condition
B. Incorrect indentation of print statements
C. Using elif without else
D. Wrong comparison operator

Solution

  1. Step 1: Check syntax of if statement

    The first if line lacks a colon at the end, which is required.
  2. Step 2: Verify other parts

    Indentation and elif usage are correct; comparison operator is valid.
  3. Final Answer:

    Missing colon after first if condition -> Option A
  4. Quick Check:

    Colon needed after if condition [OK]
Hint: Every if/elif line must end with a colon ':' [OK]
Common Mistakes:
  • Thinking elif needs else after it
  • Confusing indentation errors with missing colon
  • Believing comparison operators are wrong
5. You want an agentic AI to classify a number as 'Positive', 'Negative', or 'Zero'. Which branching structure correctly handles all cases?
hard
A. if num > 0:\n print('Positive')\nif num < 0:\n print('Negative')\nelse:\n print('Zero')
B. if num >= 0:\n print('Positive')\nelse:\n print('Negative')
C. if num > 0:\n print('Positive')\nelif num < 0:\n print('Negative')\nelse:\n print('Zero')
D. if num != 0:\n print('Positive or Negative')\nelse:\n print('Zero')

Solution

  1. Step 1: Check if all cases are covered

    if num > 0:\n print('Positive')\nelif num < 0:\n print('Negative')\nelse:\n print('Zero') checks if number is greater than zero, less than zero, and else covers zero exactly.
  2. Step 2: Verify exclusivity and correctness

    if num >= 0:\n print('Positive')\nelse:\n print('Negative') misses zero case as separate; C prints multiple lines incorrectly; D groups positive and negative together.
  3. Final Answer:

    if num > 0:\n print('Positive')\nelif num < 0:\n print('Negative')\nelse:\n print('Zero') -> Option C
  4. Quick Check:

    All cases handled exclusively in if num > 0:\n print('Positive')\nelif num < 0:\n print('Negative')\nelse:\n print('Zero') [OK]
Hint: Use if, elif, else to cover all number cases [OK]
Common Mistakes:
  • Missing zero case
  • Using multiple ifs causing multiple prints
  • Grouping positive and negative together