Functions: input() and print()
The fundamental pattern is input, then processing, then output. Keeping these stages separate makes a program easier to understand and debug.
The Program’s Three Stages
A program can be understood as a journey for data. Data enters through input, the program works with it during processing, and a result leaves through output. The functions input() and print() are useful ways to focus on the two points where a program interacts with the outside world.
The fundamental pattern is input, then processing, then output. Keeping these stages separate makes a program easier to understand and debug.
Following the Data
The diagram separates the journey into three stages. Input brings data into the program. Processing works on data inside the program. Output sends a result back out. Data can transform during processing: its type, its value, or both may change. That transformation is the part of the journey you must inspect when the result is not what you expected.
Inside and Outside
Input and output are the points where a program interacts with the outside world. Processing and data storage happen inside the program.
| Function | Direction of interaction | Role in the pattern |
|---|---|---|
| input() | Outside world into the program | Input |
| print() | Program toward the outside world | Output |
input() and print() mark opposite directions across the program boundary.
A Complete Trace
What do you think happens?
A generated example program receives the number 3 through input(), changes the value during processing, and sends the result to print(). What should you inspect first if the displayed result is unexpected?
Reveal answer
Each stage in order
The Input-Processing-Output pattern checks whether the program read the input correctly, performed the intended processing, and displayed the resulting value correctly.
Tracing the value 3
A generated program asks for a value, receives 3, performs a processing step, and uses print() to show the result. Trace the data without skipping a stage.
- 1Input
The user supplies 3. This is the point where data crosses from the outside world into the program.
- 2Storage
The program now has data inside it. This stored data is available for the processing stage.
- 3Processing
The program works with the data. The value may change during this stage, so record the value before and after the transformation.
- 4Output
The resulting data reaches
print(), which displays the program’s result outside the program.
A complete trace records the entered value, the internal transformation, and the value sent to print().
Finding the First Divergence
When the final output is wrong, do not inspect the entire program at once. Move through the stages in order. First ask whether the program read the input correctly. Then ask whether processing produced the intended result. Finally ask whether output displayed that result correctly. The first stage where the actual data differs from the expected data narrows down where the bug lives.
Common Tracing Mistakes
Common mistake Treating input, processing, and output as one undifferentiated action
What it looks likeLooking only at the final displayed result
Why it failsYou lose the point at which the data first changed unexpectedly.
Do this insteadWrite down the data after input, after processing, and at output.
Common mistake Assuming the displayed result proves that processing was correct
What it looks likeSeeing output and skipping the internal transformation
Why it failsOutput is only the final stage; a wrong value may already have been created during processing.
Do this insteadCompare the expected and actual values at every stage.
Common mistake Ignoring the program boundary
What it looks likeDescribing external input as if it were already stored inside the program
Why it failsInput is where data enters, while storage and processing happen inside the program.
Do this insteadMark the transition from outside input to internal program data.
Practice the Trace
Create a four-part trace for a generated program that receives a value through input(), changes that value during processing, and sends the result to print(). Label each part as outside input, input boundary, processing, or output boundary. Then answer: if the value is already wrong immediately after input(), which later stage should you investigate first?
Hints
- Begin with the value supplied from outside the program.
- Record the value at the point where
input()brings it inside. - Separate the transformation from the final display.
- The first incorrect stage is the most useful place to begin debugging.
Key Takeaways
- 1Every program can be viewed as input, processing, and output.
- 2
input()andprint()represent opposite directions of interaction across the program boundary. - 3Input and output interact with the outside world, while processing and data storage happen inside the program.
- 4Data may change in type, value, or both during processing.
- 5To debug a wrong result, check input, processing, and output in order and locate the first divergence.
At a glance
- Programs follow an input-processing-output pattern.
input()brings data across the boundary into a program, whileprint()sends a result outward.- Processing is the internal stage where data may transform.
- Tracing each stage helps identify where an actual result first differs from an expected result.