Python Basics for Vibe Coders, Part 4: for and while Loops and the range Trap
Learn Python for loops, range, and while. Avoid the off-by-one range mistake, stop endless while loops with Ctrl+C, and review AI-written loops.
Renaming 100 files or adding up every price in a product list is tedious for a person, but a computer needs only a few lines. That's why loops show up so often in the automation code AI writes for you. This is Part 4 of the 10-part series "Python Basics for Vibe Coders." It covers for, range, and while, plus the two mistakes people make most often with loops: an end value that's off by one, and a loop that never stops. The examples are written for Python 3.15, and I ran them in Python 3.14.2 to check the results.
Key Takeaways
forrepeats a set number of times or once per item in a list.whilerepeats as long as a condition is true.range(start, stop)does not include the stop value.range(1, 6)gives 1 through 5.- A
whileloop only ends if the code inside it changes the condition. Without that, it runs forever.- To stop a program that won't end, press
Ctrl+Cin the terminal.
A loop is a conveyor belt
When you do 10 push-ups, it helps to have someone counting "one, two, three..." for you, so you can focus on your form. A loop plays that role. Python keeps track of which round you're on.
Picture boxes on a conveyor belt being handled one at a time. The picture has a limit, though. A for loop stops on its own once every box has gone by, but a while loop keeps the belt running forever unless you install the off switch yourself.
for and range: repeat a set number of times
total = 0
for n in range(1, 6):
total = total + n
print(total)
15
for n in range(1, 6): means "assign 1, 2, 3, 4, 5 to n one at a time, and run the indented lines below for each." So total ends up as 1+2+3+4+5 = 15. The total = total + n line you saw in Part 2 updates the running sum on every pass.
for can walk through lists, not just numbers. As a preview of lists, which Part 5 covers, for f in fruits: takes each fruit from the list one by one and assigns it to f. Lines like for file in files: and for row in rows: in AI code all follow this pattern.
while: repeat while a condition is true
count = 3
while count > 0:
print(count)
count -= 1
print("Liftoff!")
3
2
1
Liftoff!
count -= 1 is shorthand for count = count - 1. Each pass lowers count by 1. When it reaches 0, count > 0 becomes false and the loop ends. That line is the switch that stops the belt.
Common trap 1: does range(1, 6) go from 1 to 6?
print(list(range(1, 6)))
[1, 2, 3, 4, 5]
The stop value, 6, is left out. If you want "add up 1 through 100" in code, you need range(1, 101). Write range(1, 100) and 100 gets dropped, so the result is 4950. Off-by-one mistakes like this don't raise any error. They just make the result slightly wrong.
Whenever you see range in AI code, check whether the last value is supposed to be included. If the start value is left out, range(5) gives 0, 1, 2, 3, 4.
Common trap 2: a while loop that never ends
If the line that changes count is missing inside while count > 0:, the condition stays true and the loop runs forever. The screen either fills up with the same output or looks frozen with no response.
- How to stop it: press
Ctrl+Cin the terminal. - How to diagnose it: check whether the
whileblock contains a line that changes the variable used in the condition. - If AI code has
while True:, look inside for a way out, such asbreak(a command that exits the loop) orreturn. Unless it's a program meant to run continuously, like a server, confirm under what conditions it exits.
How to check loop code from AI
- How many times does it run? Describe the count in words: "once per file," "5 times," and so on.
- Is the end value included? Check the stop value of
range. - How does it end? For
while, find the stopping condition and the line that changes it. - Run a small version first. If the code processes 1,000 files, ask the AI: "Also make a test version that processes only the first 3 and prints the results."
Check yourself
- What does
list(range(2, 5))return? → [2, 3, 4]. The start is included, the stop is excluded. - What happens if
countnever changes insidewhile count > 0:? → The loop never ends and keeps repeating. Stop it withCtrl+C.
Try it yourself
Add up the numbers from 1 to 100. If you get 5050, you've got it. If you get 4950, check the stop value of range again.
total = 0
for n in range(1, 101):
total = total + n
print(total)
5050
Previous and next
- Previous: Part 3: if, elif, else and the Order Bug
- Next: Part 5: Lists covers lists, which hold the values you loop over in one place, and indexes that start counting at 0.
The full series list is at the end of Part 10.