Practice: A Program That Decides Something For You
Okay so here's the thing. You've got if, elif, else, comparisons, and and/or all sitting in your toolbox now. That's genuinely most of what you need to make a program that acts like it's thinking. Today we stop collecting tools and build something with them.
We're making a program that decides whether someone can go on a hike, based on a couple questions. Nothing fancy. Super small on purpose.
The stupid-small version first
I say this every time and I'll keep saying it: break the problem into stupid-small steps. Don't try to write the whole hiking-decision program in one go. Type one input, one if statement, get it working, then add the next piece.
Start here:
```python temperature = int(input("What's the temperature outside (F)? "))
if temperature > 40: print("Good temp for a hike.") else: print("A little cold, maybe wait.") ```
Type that yourself. Don't copy-paste it in. I'm firm on this one, your fingers learn something your eyes skip right over when you just paste code in. Run it. Try a hot number, try a cold number, watch it change its answer.
Now stack on a second question
```python temperature = int(input("What's the temperature outside (F)? ")) has_water = input("Do you have water with you? (yes/no) ")
if temperature > 40 and has_water == "yes": print("Good to go, have fun.") elif temperature > 40 and has_water == "no": print("Grab water first.") else: print("Maybe hike another day.") ```
Notice what happened. We didn't rewrite anything, we just added a second condition using and, same as last lesson. That's the whole trick to building bigger programs. Small pieces, stacked.
Up here at our elevation the air's dry enough that "grab water first" is not me being dramatic, by the way. You dehydrate faster than you think you will, especially going up the canyon where it's cooler but you're working harder. Real advice hiding inside a coding example.
Add a third layer if you're feeling good
Try adding elevation gain, or distance, or whether they've got good shoes on. Whatever you want. The point isn't the hiking, the point is that you're chaining conditions to get a decision out the other end.
```python distance = int(input("How many miles is the hike? "))
if temperature > 40 and has_water == "yes" and distance < 8: print("Good to go, have fun.") elif distance >= 8: print("That's a long one, make sure you've got a plan.") else: print("Maybe hike another day.") ```
Where it'll go wrong, and that's fine
Here's a thing that trips almost everyone up at this stage. You add a new condition, run it, and the program just sits there. Doesn't crash, doesn't print, doesn't do anything. Nothing on the screen at all.
I had this happen to me years ago with a totally different kind of loop, not an if statement, but same feeling. I'd built something that was supposed to run a set number of times and instead it just ran. And ran. My old laptop's fan started sounding like it was taking off, and I sat there for a solid minute going "why is this still going" before I realized I'd never actually changed the number it was counting from. It just had no reason to stop. Ctrl-C is your friend in that moment. Hit it, the program dies right there in the terminal, no harm done. Learn that key combo now, you'll use it constantly.
Your if-statement version of that problem usually isn't an infinite loop, it's more likely one of your conditions is spelled wrong, or you used = instead of ==, or your indentation is off by one space and Python's reading it completely differently than you meant. When your program runs but gives you a weird or blank answer, don't panic and don't scrap it. Add a print statement above your if block that just shows you what the variables actually are:
``python print(temperature, has_water, distance) ``
That one line will save you ten minutes of guessing almost every time. It's not elegant. I don't care. It works and I use it constantly, my son makes fun of me for it and I have made peace with that.
What to actually build
Pick a decision from your own life. Should I water the lawn today. Should I bring a jacket. Should I order pizza or cook. Ask two or three questions with input(), run them through if/elif/else, and get the program to spit out an answer.
Keep it boring and readable. I know some of you are going to want to get clever and cram everything into one line with a bunch of ands and ors strung together. Resist that. Clever code is bad code, mostly because you're the one who has to read it again in a week, and future-you does not remember what past-you was thinking. Write it so it's obvious, even if it takes more lines.
Before next time: get your decision program running end to end, even if it's simple. If it breaks, that's not a setback, that's the interesting part, so poke at it before you ask me.