Teaching Machines to Decide

Part of the free Generative AI course on LogicWiz — module: Building the Brain's Toolkit.

Episode 4: Teaching Machines to Decide

"Intelligence is the ability to choose the right action. Wisdom is doing it a thousand times."


Nova's First Dilemma

Nova can store data and manipulate text. But right now she's passive — she can't make decisions or process things at scale.

Imagine a user asks Nova a question. She needs to:

  • Decide which prompt template to use based on the question type
  • Iterate over 1,000 articles to find relevant ones
  • Validate each result before showing it to the user

Conditionals give Nova the power to choose. Loops give her the power to repeat. Together, they transform her from a data container into something that actually thinks.


If / Elif / Else

Use if / elif / else to choose between multiple branches. Python uses indentation to define scope (not curly braces like other languages).

score = 82

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
else:
    grade = "C"

print(grade)

Conditions are evaluated sequentially — once a condition is True, the remaining branches are skipped.

{{cell:l4-try-if}}


For Loops: Processing at Scale

Nova won't handle one article at a time — she'll process hundreds. Use for to iterate over each item in a collection.

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(f"I love {fruit}")

The range() Function

range() generates a sequence of numbers. Very useful for counted loops:

for i in range(5):
    print(i)   # prints 0, 1, 2, 3, 4

for i in range(2, 6):
    print(i)   # prints 2, 3, 4, 5

The _ Placeholder

Use _ when you don't need the loop variable:

for _ in range(3):
    print("Hello!")

{{cell:l4-try-for}}

Break

break exits the loop immediately.

fruits = ["apple", "banana", "cherry", "oranges", "watermelon"]
for fruit in fruits:
    if fruit == "oranges":
        print("Yuck! My taste buds are gone!")
        break
    print(fruit)

Continue

continue skips the rest of the current iteration.

fruits = ["apple", "banana", "cherry", "oranges", "watermelon"]
for fruit in fruits:
    if fruit == "oranges":
        print("Skipping oranges")
        continue
    print(f"I love this fruit {fruit}")

{{cell:l4-try-break-continue}}


Nested Loops: Going Deeper

Sometimes Nova needs to compare every item against every other item (like finding similar articles). You can put loops inside loops. The inner loop runs completely for each iteration of the outer loop:

for i in range(3):
    for j in range(2):
        print(f"i={i}, j={j}")

{{cell:l4-try-nested}}


While Loops: Keep Going Until Done

Sometimes you don't know how many iterations you need — like when Nova keeps asking follow-up questions until the user is satisfied. Use while when you want to repeat until a condition becomes false. Make sure to update the variable inside the loop to avoid infinite loops!

count = 0
while count < 3:
    print(count)
    count += 1

{{cell:l4-try-while}}


Mission 4: Teach Nova to Think and Repeat

Time to give Nova decision-making power:

  • Writing if / elif / else conditions
  • Looping through a list and building a result
  • Using range() and the _ placeholder
  • Using break and continue correctly
  • Writing nested loops
  • Writing a while loop that terminates

Complete this mission to solidify your Data Architect title.

Next up: You've been writing code directly. But real AI systems are built from reusable pieces — functions that you call again and again. In the next episode, you'll learn to build your own superpowers that Nova can use forever...