Practice: A Program That Greets You By Name
Okay so here's the thing. You've got input(), you've got string stitching, you've got print statements. That's genuinely all you need to build something that feels like an actual program instead of an exercise. So let's build one.
Today's goal is super simple to say and a little trickier to actually type than you'd think: a program that asks for someone's name and greets them back with it. That's it. But by the time we're done it'll ask for a couple more things too, because once you feel the heist of the first version working, you'll want to push it.
Step 1: Just get a name and print it back
Open a new file. Call it greeter.py. Type this yourself, don't copy-paste it in, your fingers need to feel the parentheses and quotes to remember them later.
``python name = input("What's your name? ") print("Hey there, " + name + "!") ``
Run it. Type your name when it asks. If it prints your name back with a nice friendly "Hey there," you just did the whole assignment in its simplest form. Genuinely, that's a complete little program. Feel free to sit with that for a second.
Step 2: Ask for one more thing
Now let's have it ask something else, like where you're from.
``python name = input("What's your name? ") hometown = input("Where are you from? ") print("Hey there, " + name + "! " + hometown + " is a great town.") ``
Notice the pattern. Two input() lines up top collecting information, then one print() line at the bottom that stitches it all together. That's a shape you'll use constantly. Ask first, print later.
Step 3: Add a little math in there too
Let's ask for age and do something with it, since you already know numbers need converting.
``python name = input("What's your name? ") age = int(input("How old are you? ")) next_year = age + 1 print("Hey there, " + name + "! Next year you'll be " + str(next_year) + ".") ``
Two things to watch here, because they're the two mistakes I see most:
First one: age comes in from input() as a word, not a number, so you have to wrap it in int() right away or the math step blows up on you.
Second one: once you do math with it, next_year is a number again, so when you want to stick it into a print statement with words, you gotta wrap it back in str(). Words need words. That's the whole rule. Forget it and Python will absolutely let you know about it.
Now go break it a little on purpose
Try these, one at a time, and read what Python tells you:
- Take the
int()off the age line and see what error shows up when it tries to do math on a word. - Take a quote mark off one of your strings and see what happens.
- Add an extra space somewhere weird, like inside a variable name you're calling later, and watch it get confused.
That last one isn't a random suggestion. I spent two full hours one night convinced my code was fundamentally broken, going line by line, absolutely losing it. Turned out to be one extra space, one single character, sitting somewhere it shouldn't have been. When I finally found it I stood up, quoted an entire comedy special out loud to my empty office, just to feel like a normal person again instead of somebody who'd lost an argument with a keyboard for two hours. So. Small stuff breaks things. That's not a bug in you, that's just the job.
A trick for when it's not working
If your program runs but the output looks wrong, throw a print statement in the middle of it to check what a variable actually holds. Like this:
``python name = input("What's your name? ") print(name) # just checking what actually got stored here ``
I do this constantly. Fancier debugging tools exist and some people love them, but a print statement showing you what's really in a variable is faster nine times out of ten, and it works in literally every situation. My son thinks this makes me a caveman. He's not wrong. It still works, so I still do it.
Build your own version
Once the basic greeter runs clean, make it yours. Some ideas:
- Ask for a favorite food and mention it in the greeting.
- Ask what year they were born and calculate their age instead of asking for age directly.
- Ask two names, greet both people at once.
Keep each version boring and readable. Don't try to be clever and cram five things into one line just because you figured out how. Clever code that you can't read again in a week isn't actually clever, it's just a trap you set for future-you. Write it plain. Plain is the goal.
Before next time
Get the basic three-question greeter running on your own machine without looking at this lesson, start to finish. If it breaks, good, that's the interesting part, figure out why the computer hates you today and bring the question next class if you get stuck.