Functions: input() and print()
Every program takes input, performs processing, and produces output—this is the fundamental structure of all programs.
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 |
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
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.
Input: The user supplies 3. This is the point where data crosses from the outside world into the program.
Storage: The program now has data inside it. This stored data is available for the processing stage.
Processing: The program works with the data. The value may change during this stage, so record the value before and after the transformation.
Output: 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
Treating input, processing, and output as one undifferentiated action
You lose the point at which the data first changed unexpectedly.
Fix:
Write down the data after input, after processing, and at output.Assuming the displayed result proves that processing was correct
Output is only the final stage; a wrong value may already have been created during processing.
Fix:
Compare the expected and actual values at every stage.Ignoring the program boundary
Input is where data enters, while storage and processing happen inside the program.
Fix:
Mark 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
- Every program can be viewed as input, processing, and output.
- input() and print() represent opposite directions of interaction across the program boundary.
- Input and output interact with the outside world, while processing and data storage happen inside the program.
- Data may change in type, value, or both during processing.
- To debug a wrong result, check input, processing, and output in order and locate the first divergence.
Key Takeaways
- Programs follow an input-processing-output pattern.
- input() brings data across the boundary into a program, while print() 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.