Anatomy of a Python Loop
Learn Python loops the fun way by rolling dice, casting fireballs, and finally understanding what continue really does.
Last time, when we built our little dice-rolling function, we learned how to package up logic into reusable blocks. One die roll at a time was cool… but any tabletop nerd knows the real action starts when you need to roll lots of dice.
- 3d6 for ability scores.
- 8d6 for a fireball spell.
- Or the cruel 10d10 your DM makes you roll when things go really sideways.
So how do we tell Python: “Hey, repeat this action multiple times”?
That’s where loops come in.
What Is a Loop in Python?
A loop is Python’s way of saying: “Do this again… and again… and again… until I say stop.”
There are two main flavors:
for
loops: great for going through a sequence (like rolling N dice).while
loops: keep going while a condition is true (like rolling until you finally get a natural 20).
For Loops (Rolling Multiple Dice)
Let’s roll 3d6 for a strength score.
import random
def roll_3d6():
rolls = []
for _ in range(3):
rolls.append(random.randint(1, 6))
return sum(rolls)
print(roll_3d6()) # e.g. 12
What’s happening:
for _ in range(3):
→ repeat the block three times.- Each time, roll a d6 and add it to the list.
- Finally, sum them up.
That’s your 3d6 strength roll in one clean function.
While Loops (Rolling Until You Crit)
Now let’s simulate rolling a d20 until we finally hit that sweet, sweet 20.
def roll_until_crit():
attempts = 0
while True:
attempts += 1
result = random.randint(1, 20)
if result == 20:
return attempts
This loop keeps running until it hits the condition we want: a natural 20. Sometimes you’ll get it on the first try. Sometimes Python will make you sweat.
What Does continue
Do in Python?
Here’s the keyword that confuses a lot of beginners.
The continue
statement says: “Skip the rest of this loop iteration and jump straight to the next one.”
Let’s say you’re rolling a handful of dice but you want to ignore 1s (they don’t count toward the total).
def roll_ignore_ones(num_rolls=5):
total = 0
for _ in range(num_rolls):
result = random.randint(1, 6)
if result == 1:
continue # Skip 1s completely
total += result
return total
- If the die shows a
1
, thecontinue
makes Python skip thetotal += result
line. - Otherwise, it adds the roll to the total.
Think of it like discarding “bad rolls” before they even hit the table.
Putting It All Together: Fireball Simulator
Let’s simulate rolling 8d6 for everyone’s favorite spell.
def fireball():
total = 0
for _ in range(8):
roll = random.randint(1, 6)
total += roll
return total
Run fireball()
and boom — instant chaos.
Want to ignore snake eyes (1s)? Just add if roll == 1: continue
inside the loop.
Best Practices for Loops
Some things I’ve learned from writing real production code (and yes, they apply even if you’re not programming fireballs):
- Don’t nest too many loops — it gets unreadable fast.
- Use
break
to escape early when you’re done. - Use
continue
sparingly; if you find yourself skipping half your logic, maybe rewrite the condition. - Prefer
for
loops when you know how many times to repeat,while
loops when you don’t.
Conclusion
Loops are how Python handles repetition, and programming is often full of repetitive tasks. Whether you’re rolling 3d6, spamming fireballs, or retrying API calls in a backend service, the same principle applies: loop until you get what you need.
And if you want to level up, try combining what you’ve learned here with functions. Together, they’re the bread-and-butter of writing code that doesn’t just work once… but works every time.