DoAssignment study guide

A2.1 · Write a program with input, processing, and output

Learn to write a program with input, processing, and output through clear examples and targeted practice.

Ontario Grade 11 Computer Science

Programming Concepts and Skills

A practical structure for small Python programs | ICS3U study topic A2.1

A program can be understood as a sequence that receives information, works with it, and presents a result. These three parts are called input, processing, and output. This lesson uses small examples to practise writing that sequence clearly. The code is Python 3; other classrooms may use a different programming language, so the exact commands may differ.

What you will learn

  • Describe input, processing, and output in a simple program.
  • Identify what information a program receives, what it does with that information, and what result it presents.
  • Trace values through a short program to derive its expected output.

The three parts of a program

Input is information a program receives. A person might type a name or a number, or a program might receive information in another way. Processing is the work done with that information, such as combining text or calculating a total. Output is the result the program presents, often as text on the screen.
A useful planning question is: What does the program need to know? Next ask: What must it do with that information? Finally ask: What should the user see? Answering these questions before writing code helps ensure that the program has the information it needs and that its result is visible.
In Python 3, input() receives a line of text typed by the user. The value is text even if the user types digits. To use that value in arithmetic, convert it with int() for a whole number or float() for a number that may include a decimal part. The print() function presents output.
  • Input: information received by the program.
  • Processing: work performed on the information.
  • Output: information presented as a result.
  • Python 3 input() returns text; convert text before using it as a number.

Plan the data and trace the steps

Before writing a short program, name the inputs and decide what each one represents. Then identify the processing and the final output. This makes it easier to check that the program uses the right information. For example, a program that calculates a perimeter needs measurements, a calculation that uses those measurements, and a displayed result.
A trace is a written record of how variable values change as each line is considered. It lets you work out the expected output without claiming that the code was run. Read the program from top to bottom: record input values, apply each assignment, and then determine what print() displays.
Keep the types of values in mind. Text can be combined with other text, while arithmetic needs numeric values. In Python 3, + adds numbers when both values are numbers; when both are text, it joins the text. A conversion such as int() changes suitable digit text into a whole-number value.
  • State what each input represents before tracing the program.
  • Follow each assignment in order and update the variable's value.
  • Derive output from the traced values; do not treat an expected result as a run result.

Check that the program meets its purpose

After drafting a program, compare it with the original task. Does it receive every needed input? Does the processing use those inputs? Does the output clearly show the requested result? A program can contain valid lines of code yet still be incomplete if, for example, it calculates a value but never displays it.
Consider ordinary input values and trace them carefully. For a calculation, check the meaning of the operation and the order in which the code performs the steps. For text output, check spaces and punctuation so the result is readable. These checks use the program's stated purpose and the values shown in its code.
The examples below use direct, short sequences. Each flowchart shows the same overall pattern: input first, processing next, and output last. Use the trace tables to reason about what each program would display.
  • Match the inputs, processing, and output to the task.
  • Check calculations and readable output by tracing the stated values.
  • A calculated value must be presented if the user is meant to see it.

Trace for the perimeter example

Program pointlengthwidthperimeter
After input83not assigned
After calculation8322
Displayed result8322

Trace for the points example

Program pointfirstsecondtotal
After input1417not assigned
After addition141731
Displayed result141731

Worked example

Personalized greeting

The program should ask for a first name and display a greeting. Suppose the user types Maya. Identify the input, processing, and expected output.
Greeting program flow
Input: nameJoin greeting and nameOutput: greeting

The name is received, combined with greeting text, and displayed.

Python 3

name = input("Enter your first name: ")
greeting = "Hello, " + name + "!"
print(greeting)
  1. Identify the input
    The input is the text the user types: Maya. The input() prompt asks for a name, and the typed text is stored in the variable name.
  2. Trace the processing
    The program joins the greeting text, the name, and the exclamation mark. The spaces included in the text keep the words separated.
  3. Determine the output
    The resulting text is passed to print(), so the expected displayed line is Hello, Maya! This is derived from the stated input and code, not from an execution.
Answer: Input: the name Maya. Processing: join the greeting text with the name. Output: Hello, Maya!
Check: The printed text includes the comma, a space before the name, and the exclamation mark.

Worked example

Calculate a rectangle's perimeter

A program receives a rectangle's length and width as whole numbers, calculates its perimeter, and displays the result. Trace it for a length of 8 and a width of 3.
Perimeter program flow
Input: length, widthCalculate perimeterOutput: perimeter

Two whole-number inputs are used in a calculation and its result is displayed.

Python 3

length = int(input("Length: "))
width = int(input("Width: "))
perimeter = 2 * (length + width)
print("Perimeter:", perimeter)
  1. Receive numeric inputs
    Each input() first provides text. int() converts the digit text to a whole-number value so the program can perform arithmetic.
  2. Calculate the perimeter
    The code adds the length and width, then multiplies that sum by 2. With the stated values, the sum is 11 and the perimeter is 22.
  3. Display the result
    The print() line displays the label followed by the perimeter value. The expected output is Perimeter: 22.
Answer: The program receives 8 and 3, calculates 22, and displays Perimeter: 22.
Check: The addition combines one length and one width; multiplying by 2 accounts for both pairs of sides.

Worked example

Find the total points

A program receives points from two activities as whole numbers, adds them, and displays the total. Trace the program when the inputs are 14 and 17.
Points total flow
Input: points 1 and 2Add pointsOutput: total

Two inputs are added, then the resulting total is presented.

Python 3

first = int(input("Points from activity 1: "))
second = int(input("Points from activity 2: "))
total = first + second
print("Total points:", total)
  1. Identify the two inputs
    The program asks for points from activity 1 and activity 2. The int() conversion makes each entered whole-number value available for addition.
  2. Update the total
    The variable total receives the sum of the two inputs. Adding 14 and 17 gives 31, so total now stores 31.
  3. Trace the output
    The final line displays the words Total points: and the value stored in total. The expected output is Total points: 31.
Answer: The inputs are 14 and 17. Their total is 31, so the expected output is Total points: 31.
Check: Both input values are used once in the addition, and the resulting variable is the value displayed.

Common mistakes and how to avoid them

Using input() values directly in arithmetic, even though they are text.
Correction: For whole-number input in Python 3, convert the received text with int() before doing arithmetic.
Calculating a value but not displaying it.
Correction: Include an output step, such as print(), when the user needs to see the result.
Reporting an output that does not match the values stored in the variables.
Correction: Trace each input and assignment in order, then derive the displayed result from the final values.

Lesson summary

  • Input is information the program receives; processing is work on that information; output is the result it presents.
  • In Python 3, input() receives text. Convert suitable digit text with int() before whole-number arithmetic.
  • Trace each assignment from top to bottom to determine the expected output without claiming the code was run.
  • Check that the program receives what it needs, processes it as intended, and presents the requested result.

Check your understanding

Question 1

In the perimeter example, what does int() do to the text entered for length?
  1. It converts suitable digit text into a whole-number value.
  2. It displays the length on the screen.
  3. It adds the length to the width automatically.
  4. correctIndex: 0,
Show answer and explanation
It converts suitable digit text into a whole-number value.
int() converts suitable digit text to a whole-number value, which can then be used in the calculation.

Question 2

If the points program receives 9 and 12, what total does it display?
  1. 21
  2. 912
  3. 3
  4. correctIndex: 0
Show answer and explanation
21
The program adds the two numeric inputs: 9 plus 12 is 21.

Question 3

Which part is missing if a program calculates a total but never shows it to the user?
  1. Input
  2. Processing
  3. Output
  4. correctIndex: 2
Show answer and explanation
Output
The calculation is processing. Without a displayed result, the output part is missing.

Key terms

Input
Information a program receives.
Processing
Work a program performs using information.
Output
Information or a result the program presents.
Variable
A named place in a program where a value can be stored.
Trace
A step-by-step record of values as a program's instructions are considered.

Continue through ICS3U

View the complete ICS3U Ontario Grade 11 Computer Science curriculum and lessons

About this lesson and its review

Published by DoAssignment. This reviewed lesson follows Ontario Grade 11 Computer Science (ICS3U), expectation A2.1. It is a study resource, not an official curriculum publication.

Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.

Official curriculum reference

Report a correction or ask a question