Input from the user

Asking questions with input(), and why every answer is text.

F1.7Programming basicsGCSE12 min

Do this lesson in the simulator

So far every program has done the same thing every time it ran. A program that asks a question and uses the answer can do something different for each person. Getting information from the person using a program is called input.

Asking a question

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

name = input("What is your name? ")
print("Hello", name)
print("I am BugBot")

Run this in the simulator

When the program reaches input, it stops and waits. The text in the brackets, the prompt, appears in the console with a box after it. Type your name and press Enter. Whatever you typed is returned by input, and the assignment stores it in name.

Leave a space at the end of the prompt, inside the quotes, or your answer will be squashed up against the question.

The robot waits too

input pauses everything, including the robot. Run this and take your time answering:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

forward(30, distance=10)
colour = input("Which colour should my light be? ")
led(colour)
print("now I am", colour)
forward(30, distance=10)

Run this in the simulator

The robot drove, stopped at the question, and only carried on when you answered. Try red, blue or purple. Then try sparkly and read the error: a program that asks a person for something has to cope with whatever they type, and this one does not yet.

Asking more than once

Each input asks one question and stores one answer, so ask as many as you need, each into its own variable:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

first = input("First name? ")
team = input("Team name? ")
print(first, "from team", team, "is driving today")

Run this in the simulator

Answers are always text

Here is a problem. Run this and type 20:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

far = input("How far shall I drive, in cm? ")
print("OK,", far, "cm")
forward(50, distance=far)

Run this in the simulator

It printed OK, 20 cm happily, then crashed: forward: distance must be a number, not the text '20'. You typed a number, but input always gives back text. "20" with quotes is not the same as 20, as lesson F1.2 warned. Printing text is fine; driving by it is not.

Here is another way to see it. Run this and type 20 again:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

far = input("How far? ")
print(far * 3)

Run this in the simulator

It printed 202020. Multiplying text by 3 repeats it. No error at all, just the wrong answer: a logic error. Turning the text into a number is the next lesson.

Tasks that answer for you

When a task's program calls input, the task types the answers in for you, so it can check your program the same way every time. The answers it will type are shown at the top of the task.

Task: greet by name

Ask What is your name?, then print Hello <name>, I am BugBot using the answer, and turn the LED green. The task will answer Ada, but your program must work for any name, so do not type Ada into it.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

name = ...

Challenges

  1. Ask for a colour and a second colour, and flash the LED between them three times.
  2. Ask the user's name, then print it five times on one line using *.
  3. What happens if you just press Enter without typing anything? What does input give back?