Skip to content
Utah Community Learning

Asking the user a question with input()

About 20 minutes

Asking the User a Question with input()

Okay so here's the thing. Every program you've written so far has been a one-way conversation. You tell the computer what to print, it prints it, the end. That's fine, but it's also a little bit like talking to yourself in the car. Today we fix that. Today the program can actually ask you something and wait for you to answer.

The tool for this is input().

What input() actually does

You type this:

``python name = input("What's your name? ") ``

And here's what happens, step by step, super plain:

  1. Python prints What's your name? on the screen.
  2. The program stops. It just sits there. It's waiting on you.
  3. You type something and hit enter.
  4. Whatever you typed gets stored in the variable name.

That's it. That's the whole heist. input() shows a message, waits, and hands back whatever the person typed as a value you can use.

Try it yourself, type it in, don't copy paste it, your fingers need to feel it happen:

``python name = input("What's your name? ") print("Hey there, " + name) ``

Run it. Type your name. Watch it say hey back to you. It's a small thing but the first time it happens it feels like the program actually knows you're there.

The part everybody forgets

Okay so here's the thing that trips people up constantly: whatever input() gives you back is always a string. Always. Even if you ask someone their age and they type 34, Python is storing "34" as text, not as a number you can do math with.

Remember a few lessons back when we talked about numbers vs. words and why the computer cares? Same rule applies here, it just sneaks up on you because you don't expect it.

So if you want to do math with what someone typed, you have to convert it:

``python age = input("How old are you? ") age = int(age) next_year = age + 1 print("Next year you'll be " + str(next_year)) ``

See that int(age)? That's telling Python "no really, treat this as a whole number." And see how I had to wrap next_year back in str() down at the bottom to print it next to text? Numbers and words still don't mix without a translator. That doesn't change just because the number came from a person instead of you typing it in the code.

Why I make everyone slow down here

I want to tell you about my very first script, because this is exactly the kind of moment that got me. I wrote a little program to rename photo files by date, no user input at all, just automatic, and I was so proud of myself I basically skipped the part where you check your work carefully before you let it run wild. I ran it. It renamed all 400 photos to the same name. Which means, one by one, as it worked its way through the folder, it deleted 399 of them, because you can't have 400 files with the same name in the same folder. Gone. A whole shoot.

Why am I telling you this in the input() lesson. Because the second you let a real live human type something into your program, the program is no longer just doing what you predicted. People type nothing. People hit enter by accident. People type "thirty-four" instead of "34." Your code has to survive contact with an actual person, not just the version of events you imagined in your head. That first script of mine didn't even have user input and it still went sideways because I didn't slow down and check things before I trusted it. Once a person's typing is involved, that gets even more true, not less.

We'll get into handling bad input properly a little later on. For now, just know: the moment your program has a question mark in it, it's talking to somebody who is not you, and they will surprise you. Back up your files. I mean it. That's not a joke I tell for fun.

A stupid-small practice

Break this into tiny pieces, don't try to build the whole grocery-app-of-your-dreams in one go:

```python food = input("What's your favorite food? ") print("Nice, I like " + food + " too.")

number = input("Pick a number: ") number = int(number) doubled = number * 2 print("Double that is " + str(doubled)) ```

Type it, run it, break it on purpose. Leave off the int() and see what error message you get when you try to double a string. Read the error. It's usually telling you exactly what's wrong, it's just using computer language to do it.

Before next time

Write one program that asks for your name and your favorite number, then prints them back to you in a full sentence. If the number part gives you a weird error, that's int() trying to tell you something. Read it before you panic.

Asking the user a question with input() · Introduction to Coding with Python · Utah Community Learning