Learning LibraryCore Coding LibraryKids

input(): let the player type back

So far your program does the talking. input() lets it listen — it pauses, waits for the player to type, and hands you back whatever they wrote.

The big idea

input() waits for the player to type something, then gives that text back to your program.

See it in code

1Start simple

input() makes your program listen. It pauses, waits for the player to type, and hands back whatever they wrote. Here we ask for a name, then greet them with it:

python
print("What is your name?")
name = input()
print("Hello, " + name + "!")
Run it and type your name when it waits:
What is your name?
Hello, Alex!

The program stopped at input() until the player typed Alex, then used that answer in the greeting.

2A step further

You can ask more than once. Each input() waits for its own answer, and you save each one in its own variable to use later:

python
print("What is your name?")
name = input()
print("What is your favorite color?")
color = input()
print(name + " likes " + color + ".")
Run it and answer both questions when it waits:
What is your name?
What is your favorite color?
Alex likes blue.

Two questions, two answers, saved in name and color. That back-and-forth is how a program has a real conversation.

3In our world

Now in a text adventure. The player picks a direction — we print the question, call input() to wait, and save whatever they type in choice:

python
print("You reach a fork. Which way? Type north or south.")
choice = input()

print("You chose:", choice)
Run it and type north when it waits — then it replies:
You reach a fork. Which way? Type north or south.
You chose: north

The program paused at input() until the player typed north, then carried on using it. That's how a program has a real back-and-forth with a person.

The same idea, everywhere

input() is how programs ask people things: your name in a game, a number in a quiz, a yes-or-no answer. Anywhere a program needs you to decide, it waits for your input.

Try it yourself

Add a first question: print "What is your name?", then name = input(), and greet them with print("Hello,", name). Now your program knows who's playing.

The common mistake

Forgetting that input() always gives back text. If you ask for a number and type 5, you get the text "5", not the number 5. To do math with it, turn it into a number first with int(...).

What it unlocks

Reading input pairs with print output, and lets while loops keep asking until the answer makes sense.

Older, or want more depth? Read the Teens version →