AQA GCSE Computer Science (8525), section 3.2.7 (Input/output)
Input and output
- Input is data going into the program, here from the keyboard.
- Output is data going out, here to the computer display.
In pseudo-code
OUTPUT "What is your name?"
name ← USERINPUT
OUTPUT "Hello " + name
USERINPUT waits for the user to type something and press Enter. The value is then stored in a variable.
In Python
name = input("What is your name? ")
print("Hello", name)
input() always gives a string. To use it as a number, convert it:
age = int(input("Age? ")) # whole number
height = float(input("Height? ")) # real number
print("Next year you will be", age + 1)
Prompts
Always tell the user what to enter. A prompt such as "Enter a number from 1 to 10" prevents mistakes.
Formatting output
You can join (concatenate) text and values in output. In Python, convert numbers to strings, or use commas in print:
score = 12
print("Your score is " + str(score))
print("Your score is", score)
Why it matters
Programs need input to be interactive, and output to show results. Without them the user cannot get anything useful from a program.
Common mistake: forgetting to convert input from a string. In Python, input() returns text, so "5" + "5" gives "55". Also, remember to output a clear message so the user knows what to type.
Exam tip: when writing an input/output answer, always include a prompt and store the input in a variable. Convert to a number if you will do arithmetic on it.
Check yourself
What does USERINPUT do?
It gets data typed by the user and lets the program store it in a variable.
What data type does Python input() return?
A string.
Why should a program display a prompt before asking for input?
So the user knows what they are expected to enter.