Episode 3: Loops and Functions, or How to Stop Copy-Pasting Yourself Into Madness

Sora

If Episode 2 was about teaching your computer how to remember things and make decisions, Episode 3 is about teaching it how to do the same thing more than once without you losing your will to live.

Because sooner or later, every beginner writes something like:

“I need to print 10 lines…”

…and then types the same line 10 times like a determined raccoon building a bridge out of snack wrappers.

That is the moment you are ready for loops and functions.

Also, if you want the original kickoff for this whole series, it starts here: https://medium.com/@DaveLumAI/back-to-basics-cs-101-explained-like-youre-holding-the-syllabus-upside-down-554961c6e0b8


Loops: Controlled Repetition (So You Can Keep Your Sanity)

A loop is the computer equivalent of:

“Do this again.”

But with rules.

And rules are important because computers are incredibly talented at following instructions in the most unhelpful way possible.

Grok

The Two Main Loop Flavors

For loops: “Do this for each thing in this group.”

While loops: “Keep doing this while this condition stays true.”

That is the whole idea.

The rest is learning the art of not accidentally creating an immortal machine spirit that prints “hello” forever.

The Classic Beginner Trap: The Infinite Loop

An infinite loop happens when you tell the computer:

“Keep going until X happens…”

…and then you never do anything that makes X happen.

The computer does not get bored.

The computer does not get hungry.

The computer does not suddenly decide to go for a walk and think about its choices.

It just keeps going, happily chewing electricity like a goat in a cardboard factory.

Here is a friendly, tiny example in Python:

# Print the numbers 1 to 5
for i in range(1, 6):
print(i)

A loop like this is great because it has a clear start, a clear end, and it does not become your new roommate.

Now compare that to this:

# WARNING: This loop never stops
while True:
print("I live here now.")

If you run that, your computer will do exactly what you asked and then stare at you like, “Why are you upset? You said forever.”

Gemini

The Secret Superpower of Loops

Loops are not just “repeat this.” They are pattern engines.

They let you:

  • process lists of names
  • scan files line by line
  • try again when a user enters nonsense
  • simulate things
  • build simple games
  • do boring tasks so you do not have to

In other words: loops are how you stop writing code like it’s 1997 and you’re paid by the keystroke.


Functions: Reusable Thought Packets

A function is a named bundle of logic that you can run whenever you want.

Think of it like a little machine you build once, then reuse forever.

Not because you’re lazy.

Because you are tired of fixing the same bug in five places.

NightCafe

Why Functions Save Your Future Self

Without functions, your code turns into one long scroll of instructions where everything depends on everything else, and changing anything feels like pulling one thread on a sweater.

With functions, you can say:

  • “This part gets the input.”
  • “This part calculates the result.”
  • “This part prints the output.”
  • “This part handles errors when reality shows up.”

And suddenly your program stops feeling like a haunted house.

Here is a friendly example:

def greet(name):
return f"Hello, {name}!"

message = greet("Ada")
print(message)

A few big ideas are hiding in there:

  • Inputs are the things you pass in (like name)
  • Outputs are what you get back (the return value)
  • A function name is basically a label that says, “This chunk has a job”

The Best Part: Combining Loops and Functions

This is where programming starts to feel like actual power.

You write a function once, then loop over a bunch of data and use that function repeatedly.

Example:

def square(n):
return n * n

numbers = [1, 2, 3, 4, 5]
for n in numbers:
print(square(n))

That is the moment you stop being someone who types code and start being someone who builds tools.


When Loops Go Wrong (And They Will)

Loops tend to fail in three main ways:

1) Off-by-one errors

You meant 10 times.

You did 9.

Or 11.

And now your output looks like it is missing a tooth.

2) The loop condition never changes

The loop is waiting for something.

You never update the thing.

The loop becomes eternal.

3) You put too much inside the loop

If your loop contains 40 lines of logic, multiple nested conditionals, and a small existential crisis, it is time to break it up into functions.

A good rule:

If you cannot explain your loop out loud without needing to point at the screen like a detective, simplify it.


The Quiet Lesson Hidden in Episode 3

Episode 3 is not really about syntax.

It is about structure.

Loops teach you to repeat responsibly.

Functions teach you to organize responsibility.

And together they teach you the most underrated skill in programming:

Write code that you will not hate in two weeks.

If you want a coding environment that makes experimenting with loops and functions feel smoother (especially when you’re iterating fast), check out https://cursor.com/


Your Turn

Drop a comment with one of these:

  • The funniest infinite loop you accidentally created (bonus points if it spammed your screen like a possessed slot machine)
  • A function you wish existed in real life (mine would be refill_coffee() but with zero side effects)
  • The moment loops finally “clicked” for you

And if you want more episodes in this series, follow along so you do not miss the next one.


Deep Dream Generator

Art Prompt (Pointillism): Create a luminous outdoor scene built entirely from tiny, deliberate dots of color, where sunlight sparkles across water and soft shadows pool beneath leafy trees. The palette should balance warm honeyed yellows and coral-pinks with cool violets and blue-greens, creating shimmering optical blends when viewed from a distance. Compose the scene with calm, classical order: a broad open foreground, a gentle middle band of activity and texture, and a serene horizon that feels airy and expansive. Keep edges crisp through dot density rather than outlines, and let the atmosphere feel bright, leisurely, and quietly majestic, like a perfect afternoon that never hurries.

Video Prompt: Start with a burst of tiny, glowing color-dots popping onto a blank canvas in rhythmic waves, as if the scene is being assembled by light itself. The dots swirl into trees, water, and sunlit ground, then softly reconfigure in looping patterns: sparkles ripple across the water, shadows dapple and shift under leaves, and the whole image subtly “breathes” as dot clusters brighten and dim in time. Add gentle parallax depth by letting foreground dot-fields drift faster than the background, with occasional punchy micro-zooms into sparkling highlights before snapping back to the full composition. Keep everything crisp, luminous, and mesmerizing, with the motion driven by dot choreography and shimmering atmosphere rather than camera panning.

Song Recommendations for the Video:

  • Dayvan Cowboy — Boards of Canada
  • Oblivion — Grimes