Practice: A Loop That Lists Things Out For You
Okay so here's the thing. You've got for loops, you've got while loops, you know how to stop one when it goes rogue on you. Now we're just going to sit down and use it. No new concepts this lesson. Just typing, breaking things, and fixing them, which is honestly most of what coding is.
We're going to build a super small program that lists out something you actually care about. Not a fake example about widgets. Pick something real. Your grocery list. The names of everyone in your family. Every canyon trail you want to hike this summer. Doesn't matter, as long as it's yours.
Step one: make the list
Open up your editor and type this. Type it yourself, don't copy and paste it in. I've said this before and I'll keep saying it because it's true: your fingers learn something your eyes skip right past.
``python things = ["milk", "eggs", "bread", "cheese"] ``
Swap in your own four or five items. If you want to do a hiking list, do a hiking list.
Step two: loop over it
``python for item in things: print(item) ``
Run it. You should see your items print out one per line. If that happened, congratulations, you just told the computer "go through this list and show me each one" and it did it without complaining. That's the whole heist. That's loops.
Step three: make it useful, not just correct
A list that prints itself is fine but kind of boring. Let's make it print something with a little context, so it feels like an actual program and not just a robot reading a grocery receipt.
``python for item in things: print("Don't forget:", item) ``
Small change, but now it reads like a person wrote it for a reason. This is the difference between code that technically works and code you'd actually want to use. Both count as "working," but only one of them feels good to run.
Step four: count while you're at it
Let's add a counter, because counting things while you loop over them comes up constantly once you start writing real programs.
```python count = 0 for item in things: print("Don't forget:", item) count = count + 1
print("That's", count, "things.") ```
Run it. Watch the number match your list length. If it doesn't match, something's off, and that's genuinely the interesting part, not the annoying part. Go find it.
Where this always goes sideways
Okay so here's the thing that will probably happen to at least half of you tonight. You'll type this whole thing out, run it, and get nothing. No output, no error, nothing. And you will stare at it convinced Python is broken or the lesson is wrong or you're missing some huge concept.
I had a night exactly like this once. Two hours, convinced my code was fundamentally broken, going line by line like I was defusing something. Turned out it was one extra space. One. A single space where Python didn't want one, silently making the whole thing behave differently than I expected. When I finally found it I just sat there in my office and quoted an entire comedy special out loud to the empty room, because I needed to feel like a person again after staring at a blinking cursor for two hours over a space bar.
So when your thing doesn't run right tonight, before you assume you don't understand loops, check the boring stuff first:
- Is your
forline ending in a colon? - Is the line under it indented, and indented the same amount as the other lines in the loop?
- Did you spell
thingsthe same way every time? Python cares about that.thingsandThingsare two different words to it.
Ninety percent of the time it's something dumb like that. Not a gap in your understanding. A typo. Everybody's first instinct is "I must be bad at this," and honestly, no. You're just debugging, which is the actual job, forever, even for people who've been doing this a long time.
If you want to push it a little further
Try printing your list backwards, or only printing items longer than four letters, or adding a print("Done!") after the loop finishes so you know it actually got through everything. None of that is required. It's just what I'd poke at if I were you and had ten extra minutes.
And if you get an error message, read the actual words in it before you panic. It's usually telling you exactly what's wrong, just in kind of a flat, unhelpful tone. Like a teenager.
Before next time
Get this running with your own list of five or six things, and try adding one more print line somewhere that isn't in this lesson at all, just to see what happens when you mess with it on purpose.