Build Small Interactive Python Programs
Input, Process, Output
Almost every program follows the same three-step shape, called input, process, output (IPO for short):
- Input: get the data, here with
input(). - Process: work with it: convert, calculate, combine.
- Output: show the result with
print().
- Start: Start
- Input or output: Input: read a number
- Step: Process: multiply it by 2
- Input or output: Output: show the result
- End: End
You know these shapes from chapter 3: input and output are parallelograms, and the processing step is a rectangle. Now each shape becomes one line of code:
number = int(input())
double = number * 2
print("Double:", double)Double: 42
Think of a juice machine: fruit goes in (input), the machine squeezes it (process), and juice comes out (output).
Plan before you code
For every program in this topic, start by answering three questions:
- What goes in? Which values does the user type, and are they text, whole numbers or decimals?
- What happens? Which calculation or combination?
- What comes out? What exactly should the screen show?
Once you can answer them, the code is just three short groups of lines. This is the 4-step method from chapter 3, made concrete.
Input, process, output: plan all three, then write the code.