Concepts / Functions: input() and print()

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.

  • Programming

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.

Key point

The fundamental pattern is input, then processing, then output. Keeping these stages separate makes a program easier to understand and debug.

Following the Data

Outside inputentered datainput()data entersProcessingdata changesprint()result leavesOutside outputdisplayed resultenterspasses datapasses resultdisplays
How does data move from the outside world into a program, through processing, and back out as a result?

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

Outside worldexternal interactioninput()entry boundaryProgram datastored insideProcessingwork insideprint()exit boundaryprovides inputenters programis processedproduces resultreturns output
What belongs inside the program, and where do input and output cross the boundary to interact with the outside world?
Definition

Input and output are the points where a program interacts with the outside world. Processing and data storage happen inside the program.

FunctionDirection of interactionRole in the pattern
input()Outside world into the programInput
print()Program toward the outside worldOutput

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?

  • Only the input stage

  • Only the processing stage

  • Only the output stage

  • Each stage in order

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.

3entered valueInput valueread by input()Processed valuetransformed insideDisplayed resultshown by print()input() readsprocessing transformsprint() displays
What happens next to the entered value at each step until print() displays the final result?
Worked example

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.

  1. 1
    Input

    The user supplies 3. This is the point where data crosses from the outside world into the program.

  2. 2
    Storage

    The program now has data inside it. This stored data is available for the processing stage.

  3. 3
    Processing

    The program works with the data. The value may change during this stage, so record the value before and after the transformation.

  4. 4
    Output

    The resulting data reaches print(), which displays the program’s result outside the program.

Result

A complete trace records the entered value, the internal transformation, and the value sent to print().

Finding the First Divergence

Check inputWas data read correctly?Check processingWas the value calculatedcorrectly?Check outputWas the result displayedcorrectly?First wrong stagelikely bug locationinput is correctprocessing is correctoutput differsinput differsprocessing differs
At which stage—input, processing, or output—does the program’s data first differ from what was expected?

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 like

    Looking only at the final displayed result

    Why it fails

    You lose the point at which the data first changed unexpectedly.

    Do this instead

    Write down the data after input, after processing, and at output.

  • Common mistake

    Assuming the displayed result proves that processing was correct

    What it looks like

    Seeing output and skipping the internal transformation

    Why it fails

    Output is only the final stage; a wrong value may already have been created during processing.

    Do this instead

    Compare the expected and actual values at every stage.

  • Common mistake

    Ignoring the program boundary

    What it looks like

    Describing external input as if it were already stored inside the program

    Why it fails

    Input is where data enters, while storage and processing happen inside the program.

    Do this instead

    Mark the transition from outside input to internal program data.

Practice the Trace

MEDIUM

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

  1. 1
    Every program can be viewed as input, processing, and output.
  2. 2
    input() and print() represent opposite directions of interaction across the program boundary.
  3. 3
    Input and output interact with the outside world, while processing and data storage happen inside the program.
  4. 4
    Data may change in type, value, or both during processing.
  5. 5
    To 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, 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.

Related concepts

More concepts starting with F