Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Computational Thinking · Grade 6-8

Input/Output

Input/Output is how a program communicates across its own boundary: input is data it receives from the outside world (keyboard, file, sensor, network), and output is the results it sends back (screen, file, printer, device).


Quick Answer

Input/Output is how a program communicates across its own boundary: input is data it receives from the outside world (keyboard, file, sensor, network), and output is the results it sends back (screen, file, printer, device). You use it whenever you describe what a program takes in and what it gives back, such as a thermostat that reads 18 °C from a sensor and switches a heater on. Keyboard input arrives as text, so a typed 5 must be converted to a number before you do arithmetic with it.

Why This Matters

A program with no input gives the same result every time, and one with no output keeps its result to itself. Labeling inputs and outputs tells you what a program depends on, what you must check (a typed age might be 'twelve' or -3), and what it promises to deliver. Many real bugs live at this boundary: text that was never converted, values nobody checked, prompts shown in the wrong order.

Intuitive Explanation

Draw a box and call it the program. Everything that crosses the edge of the box going in is input: the keys you press, a temperature from a sensor, a file the program opens, a message from a server. Everything crossing going out is output: words on the screen, a sound, a saved file, a signal that starts a motor. What happens inside the box is processing.

Take a tip calculator. It asks for the bill and you type 40. That 40 arrives as the text '40', two characters, not a number. The program converts it, works out 40 × 0.15 = 6, and prints 'Tip: 6.00'. Skip the conversion and Python refuses to multiply the text by 0.15, while in JavaScript '40' + 6 gives '406'. Input also cannot be trusted: someone will type 'forty' or leave the box empty, so a careful program checks what arrives before using it.

Not every value in a program is input or output. The variable tip = bill × 0.15 is created and used inside the box; it never crosses the edge. A value written into the code by the programmer, such as the 0.15, is not input either, because it does not arrive from outside while the program runs. Computing a result is still processing until the program shows, saves or sends it.

Think of it like this. A program works like a restaurant kitchen behind a serving window: orders come in through the window (input), meals go out through it (output), and the cooking in between stays out of sight. Like a real order slip, input can be smudged or wrong, so the cook reads it carefully before starting.

Core idea. Programs are useless without I/O—they transform input into output.


When to Use

Think in input/output when a problem mentions where data comes from or goes to: a user types, a sensor reads, a file is opened, a result is displayed, printed or saved. Labeling each piece as in or out is the first step of planning any program. If the question is how the screen is laid out (buttons, menus), that is User Interface. If it is about the mechanics of opening, reading and closing files, that is File Operations.

Tip. Ask: Is the question about data entering the program from outside, or results leaving it for a screen, file, or device?

How to Recognize It

Before labelling something input or output, picture the edge of the program and ask which way the data crosses it.

  1. Does the data come from outside the program, such as a keyboard, sensor, file or the internet?

    Yes means it is input. Data made inside the program, like a total it works out, is neither until it is sent out.

  2. Does the program send out something a person or device can see, hear or store?

    Yes means output, whether it is text on a screen, a sound, a saved file or a signal to a motor.

  3. Is the question about when the program reacts, such as the moment a key is pressed?

    Yes points to Event, the happening that triggers code. The data that comes with it, like which key was pressed, is the input.

  4. Is the question about the buttons and layout a person sees?

    Yes points to User Interface, which is how the input and output are presented to a person, not the data itself.

Input/Output vs Event vs User Interface vs File Operations

A single key press can count as an event, an input and part of a user interface at once, so these blur. Ask whether you mean data crossing in or out, the moment that triggers code, the screen design, or saving to storage.

Input/Output

Meaning
Input/output is the data a program takes in from outside and the results it sends back out.
Key test
Which way does the data cross the program's edge?
Formula
P:I→O
Example
A thermometer program reads 21 from a sensor (input) and shows '21 degrees' on a screen (output).

Event

Meaning
An event is a happening, such as a click, a key press or a timer running out, that the program can respond to.
Key test
It is about when code runs, not what data moves.
Example
When the space bar is pressed, the character jumps.

User Interface

Meaning
A user interface is the set of buttons, menus, text boxes and screens through which a person uses a program.
Key test
It is about what the person sees and touches.
Example
A calculator app's grid of number buttons and its display bar.

File Operations

Meaning
File operations read data from files and write data to files, so that it is still there after the program closes.
Key test
The data is kept on storage between runs.
Example
A game writes the high score to a file and reads it back the next time it starts.

Formula & Notation

Worked Examples

Example 1 — Label a thermostat's inputs and outputs

Easy

A smart thermostat program reads the room temperature from a sensor (18 °C), reads the target temperature (20 °C) from its settings file, switches the heater on if the room is colder than the target, shows the room temperature on its screen, and adds the line '18 ON' to a log file. Label every input and output, and say what the program does with 18 and 20.
Show the steps and the answer
  1. List what enters the program: the sensor reading 18 and the target 20 from the settings file.

    Both come from outside the program's code, one from a device and one from storage, so both are input.

  2. Do the processing: 18 < 20 is true, so the decision is heater on.

    The comparison happens inside the program and crosses no boundary, so it is neither input nor output.

  3. List what leaves the program: the signal that switches the heater on, the 18 shown on the screen, and the line '18 ON' written to the log file.

    Output is anything sent out, to a device, a screen or a file, not just text on a display.

Answer: Inputs: the sensor reading 18 °C and the target 20 °C from the settings file. Processing: 18 < 20, so heat. Outputs: the heater-on signal, 18 on the screen, and '18 ON' in the log file.

Takeaway: Files can be input in one step and output in another. Label each piece of data by the direction it crosses the program's edge.

Example 2 — A value in the code is not input

Standard

Program A: price = 8, tickets = 3, total = price × tickets, print total. Program B: price = 8, tickets = the number the user types, total = price × tickets, print total. When the user types 3 into B, both print 24. Which values are inputs in each program?
Show the steps and the answer
  1. In A, look for anything that comes from outside: price and tickets are both written into the code.

    A value typed by the programmer is part of the program itself. A runs the same way every time and prints 24 forever.

  2. In B, tickets comes from the keyboard, so it is input; price is still a fixed value in the code.

    Whether a value is input depends on where it comes from while the program runs, not on whether it is a number or has a name.

  3. Run B with 5 instead: total = 8 × 5 = 40, and it prints 40.

    A different input changes the output. A cannot do that unless someone edits its code.

  4. Label the outputs: print total in both programs.

    Both send a result to the screen, so both have output, but only B has input.

Answer: A has no input; its only output is the printed 24. B has one input, tickets from the keyboard, and one output, the printed total (24 for 3, 40 for 5).

Takeaway: A value written in the code is not input, even when it looks like data. Input arrives from outside while the program is running.

Example 3 — Find the mistake: 7 + 8 = 78

Application

A program asks for two quiz scores and adds them: a = input('First score: '), b = input('Second score: '), print(a + b). Kai types 7 and 8 and the program prints 78. Kai says: "The addition is broken." Find the mistake and correct it.
Show the steps and the answer
  1. Check what input() hands back: the text '7' and the text '8', not numbers.

    Keyboard input arrives as characters. The screen shows 7, but the program holds a one-character string.

  2. See what + does to two strings: '7' + '8' joins them into '78'.

    For text, + means 'stick together', so no addition ever happened. The bug is the missing conversion, not the + sign.

  3. Convert each input: a = int(input('First score: ')), and the same for b.

    int() turns the text '7' into the number 7, so + now means arithmetic.

  4. Run it again with 7 and 8: it prints 15.

    Same input, correct output, because 7 + 8 = 15.

  5. Try typing 'seven': int('seven') stops with an error, so check the input before converting it.

    Conversion fixes the arithmetic, but input from a person still has to be checked before the program relies on it.

Answer: Convert both inputs with int() before adding. Then 7 and 8 give 15.

Takeaway: Treat everything typed at the keyboard as text until you convert it.

Where You'll See It

  • Vending machine

    Your button press and coins are the input; the snack that drops out and the change returned are the output. The machine transforms the input (money + selection) into output (product), exactly like a program's I/O.

  • Typing a name into a video game

    The keyboard sends your name as input text into the game, and the screen displays 'Welcome, <name>!' as output. The name arrives as a string of characters, which is why you can't do math on it without converting it first.

  • Saving a screenshot

    Pressing the screenshot key is an input event; writing the image file to your disk is output to a storage device, showing that output isn't only the screen.

Common Mistakes

  1. The slip: Input typed at the keyboard is already a number I can add.

    Why it's tempting: It looks like a number on screen (you typed '5'), so it feels ready for arithmetic.

    Instead: Keyboard input arrives as text (a string); you must convert it to a number (e.g. int() or Number()) before doing math, or '5' + '5' becomes '55' instead of 10.

  2. The slip: I can trust whatever the user types, so I don't need to check it.

    Why it's tempting: During your own testing you always type sensible values, so bad input never appears.

    Instead: Always validate input before using it; unexpected input (letters where a number is expected, empty input) can crash the program or create security holes.

  3. The slip: Output only means printing to the screen.

    Why it's tempting: Screen output is the kind you see most, so it feels like the only kind.

    Instead: Output is anything a program sends out: to a screen, a file, a printer, a speaker, a network connection, or another program.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. A program reads the temperature from a sensor and shows a warning on a screen. Name the input and the output.

    Hint: Input comes into the program; output goes out of it.

  2. Trace: `name = input("Name? ")`, the user types Mia, then `print("Hello, " + name)`. What appears on the screen, in order?

    Hint: The prompt inside `input` is shown before the user types.

  3. `a = input()` receives "6" and `b = input()` receives "4". What does `print(a + b)` show? What does `print(int(a) + int(b))` show?

    Hint: What type does typed input arrive as?

  4. A program asks for 3 test scores, one at a time, then prints their average. The user types 80, 90 and 85. List the inputs and the output.

    Hint: Add the three scores, then divide by how many there are.

  5. A program saves high scores to scores.txt so they are still there the next time it runs. Is that general input/output, or file operations?

    Hint: Is the question about data on the screen, or data kept after the program closes?

  6. A program asks "How many tickets?" and the user types "two". The program crashes on `int(answer)`. What was missing?

    Hint: What should a program do before trusting what a user typed?

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

Can the output of one program be the input of another?

Yes, and it happens constantly. A weather station program writes its readings to a file, and a website's program reads that file as input to draw a chart. When you send a message, your phone's output travels over the internet and becomes the input of your friend's phone. Much of computing is programs passing data along to each other in chains like this.

Is a game controller's vibration a kind of output?

Yes. Output is anything a program sends out to the world, and a buzz in your hands counts as much as a picture on a screen. A controller is both an input and an output device: the buttons and joysticks send your moves in, and the motors that shake it carry the game's signal out. A touchscreen is the same, taking taps in and showing pictures out.

Why does a program sometimes stop and wait at an input line?

Because it cannot go on without the data. When Python reaches name = input('Name? '), it stops and waits, however long it takes, until the user presses Enter. Reading from the internet or a slow disk also makes a program wait. Good programs show a message or a loading sign during the wait, so the user knows the program has not frozen.

Learning Path

← Before

No prerequisites
Input/Output

You are here

This is a starting point: no earlier concept is needed. Input/Output gives you the in, process, out picture of every program, plus the habit of converting and checking what comes in. Next, User Interface designs how a person gives input and reads output, and File Operations makes output last after the program closes.

See Also