Input from the user
Asking questions with input(), and why every answer is text.
Covers OCR J277 2.2.1 Programming fundamentals · AQA 8525 3.2.7 Input/output · Edexcel 1CP2 6.4.1 Responding to user input
Do this lesson in the simulatorPrint it: worksheet (PDF) · answers (PDF) · as a web page
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")
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)
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")
Joining text to an answer
A comma in print always adds a space, so print("Hello", name, ".") prints Hello Ada . with a gap before the full stop. To put a mark straight after the answer, join the pieces into one piece of text with +:
# 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 + ".")
Type Ada and it prints Hello Ada. Joining adds no spaces of its own, so any space you want goes inside the quotes, like the one after Hello. Lesson F1.8 shows what else + does to text.
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)
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)
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
- Ask for a colour and a second colour, and flash the LED between them three times.
- Ask the user's name, then print it five times on one line using
*. - What happens if you just press Enter without typing anything? What does
inputgive back?
Past paper questions on this
Real exam questions that test this, each worked through with a program you can run.
- AQA 8525 June 2025 Paper 1, Question 2: A loyalty card with MOD and elif, and the value of the free items 7 marks
- AQA 8525 June 2024 Paper 1, Question 1: LEN, POSITION, a data type, assignment, and a two line program 6 marks
- AQA 8525 June 2024 Paper 1, Question 6: Essay marks with penalties for late essays, never below 0 7 marks
- AQA 8525 June 2024 Paper 1, Question 7: Build a stock code with string indexing and concatenation 4 marks
- AQA 8525 June 2023 Paper 1, Question 7: A ticket price with a group discount 6 marks
- AQA 8525 June 2022 Paper 1, Question 7: Ask for an email address twice and check that they match 5 marks
- AQA 8525 Sample Paper 1, Question 5: A taxi fare: 2 pounds a passenger plus 1.50 a kilometre 7 marks
- OCR J277 June 2025 Paper 2, Question 6(a): Input two passwords and check that they match 4 marks