Practice: A Tiny Grocery-Total Program
Okay so here's the thing. You've got variables, you've got math, you've got names that make sense. That's genuinely most of the toolbox for this project. Time to actually build something instead of just poking at the language.
We're going to make a tiny program that adds up a grocery trip. Not a real receipt-scanning, tax-calculating thing. Just something that takes a few item prices and tells you the total. Super small scope on purpose.
Here's why I'm having you build this specific thing and not something flashier: my brother Douglas once asked me to "just make a quick program" to sort his fishing trip data, and six hours later I'd built this enormous thing with menus and categories and I don't even remember what else, and he never used it. Not once. I did that project for me, not him, and I'm fine admitting that now. But the lesson underneath it is real: scope creep will eat your whole night if you let it. So today, we're keeping this stupid small on purpose. A few prices, a total, maybe a message at the end. That's it. Resist the urge to add fifteen features before you've even got three lines working.
Step 1: Open a new file
Make a new file in your editor, save it as grocery_total.py. Remember from a couple lessons back, where things live matters, so put it somewhere you'll find it again. Not just floating in Downloads with four hundred other things.
Step 2: Make variables for your items
Type this yourself. Don't copy and paste it, I mean it, your fingers need to feel these characters go by or it doesn't stick the same way.
``python milk = 3.49 eggs = 4.29 bread = 2.99 cheese = 6.79 ``
Four variables, four prices, decimals because we're dealing with money and money has cents.
Step 3: Add them up
``python total = milk + eggs + bread + cheese ``
One line. That's the whole "hard part." I told you, math in this class is mostly just plus signs and patience.
Step 4: Print the total
``python print("Your total is:", total) ``
Run it. You should see something like Your total is: 17.56. If you see that number, congratulations, you just built a program that does a real thing a real person would actually use. That's a heist and you should feel it.
Step 5: Make it talk a little
Let's have it comment on the total, because a program that just spits out a number is fine, but a program with a tiny bit of personality is more fun to build and honestly more fun to demo to your family later.
``python if total > 20: print("That's a big trip.") else: print("Not bad.") ``
We haven't officially covered if yet, that's coming up, so don't stress about the syntax. Just type it, run it, and watch it work. Sometimes the best way to get curious about the next topic is to see it do something cool before you understand all of it.
A confession, since we're on the subject of grocery totals
Camille, my wife, asked me to write something like this for real once, after a Costco run got a little out of hand. I built it, it worked great, and it very helpfully informed us that we spend way too much money on cheese. Which, yeah. We already knew that. The computer did not need to tell us. But it did, because that's what the numbers were, and there's something about seeing it printed out in black and white that hits different than just knowing it in the back of your head.
That's kind of the whole point of this exercise, honestly. It's not really about groceries. It's about seeing how a handful of variables and one line of math turn into something that tells you true things about your actual life. That's the moment coding stops being an exercise and starts being useful.
If something breaks
If you get an error, read it. Actually read the words, don't just panic-scroll past it. Python is usually telling you exactly what's wrong, just in a slightly annoyed tone. Common ones here: a typo in a variable name, a missing comma inside the parentheses, or quotes that don't match up. Check those first before you assume the whole thing is broken.
And if it runs but the total looks wrong, add a print() after each variable to see what Python thinks each price actually is. I use print statements to debug constantly, my son calls it caveman debugging, I call it "it works and I understand every step of it." Both true.
Before next time
Add two more items to your list and get a new total. Try changing that 20 in the if-statement to some other number and see how it changes what gets printed. Small tweaks, same file, no pressure to make it fancy.