Python Arena
Step 1 of 4

Asking the user

The programs in the first module print the same thing every time they run. Most useful programs also take input: information the user gives while the program runs. In Python, the command input reads one line that the user types.

city = input("Where do you live? ")
print(city)
Input
Lisbon
Output
Where do you live? Lisbon
Lisbon

Here is what happens, line by line:

  • input("Where do you live? ") shows the text in the parentheses and then waits. This text is the prompt: it tells the user what to type. The program stops right there until the user types a line and presses Enter.
  • What the user typed becomes the result of input. The city = in front of it stores that text under the name city, so the program can use it later. A name that holds a value like this is a variable. The next step looks at variables more closely.
  • print(city) prints what is stored in city: the text the user typed. There are no quotes around city, because the program should print the value, not the word "city".

What a run looks like

On a computer, the user types Lisbon right after the prompt, on the same line, and the screen shows it there. The examples on this site show it the same way: the Input block above is what the user types, and in the output it appears right after the prompt, followed by a line break. When you check a program here, the tests type the input for you, a different line in each test.

Notice the space at the end of the prompt, just before the closing quote. Without it, the answer sticks to the question: Where do you live?Lisbon. The space is part of the prompt, so it goes inside the quotes.

input without a prompt

The parentheses can also be empty. Then input() shows nothing at all and simply waits, so the typed line appears on a line of its own:

print("Type a word and press Enter.")
word = input()
print(word)
Input
hello
Output
Type a word and press Enter.
hello
hello

The first hello is what the user typed, and the second is what the program printed. Both work, but a prompt is usually friendlier: the user sees what to type right where they type it.

Your task

Challenge 1 of 3

Fill in the blank so the program asks for the user's favorite color and then prints it. If the user types green, the run looks like this:

Favorite color: green
green

Challenge 2

Write a program that asks for a word with the prompt Word: and prints it between two lines of five stars. If the user types hello, the run looks like this:

Word: hello
*****
hello
*****

Challenge 3

This program asks its question with print, so the user has to type the answer on the next line. Change it so the question is the prompt of input and the answer is typed on the same line. If the user types 2, the run should look like this:

How many cats do you have? 2
2