Output and Input

Make programs talk to the user with print and input.

  • Define and explain Output and Input in your own words
  • Use key terms such as function accurately
  • Apply what you have learned to new examples and questions
  • Avoid the common mistakes learners make with this topic

This lesson focuses on Output and Input: make programs talk to the user with print and input.

Definition: Output and Input

Make programs talk to the user with print and input.

Key ideas

Programs are input, process, output

Nearly every program follows the same shape: take something in, do something with it, and show a result. A quiz program inputs answers, processes them against the correct ones, and outputs a score. Seeing this pattern helps you plan any program before you type.

Selection lets programs decide

An if statement asks a true-or-false question and runs different code for each answer. elif chains let you test several possibilities in order, and else catches everything left over. Getting the order of conditions right matters — the first true condition wins.

Key term — function: A named block of code that performs one task and can be called repeatedly, often with different inputs called parameters.

Trace the Total

What does this program print? First line: total = 0. Next two lines: for n in range(1, 6): then total = total + n (indented). Last line: print(total).

range(1, 6) produces the numbers 1, 2, 3, 4, 5 — the stop value 6 is not included. Start: total = 0. First iteration, n = 1, so total becomes 0 + 1 = 1. Second iteration, n = 2: total becomes 1 + 2 = 3. Third iteration, n = 3: total becomes 3 + 3 = 6. Fourth iteration, n = 4: total becomes 6 + 4 = 10. Fifth iteration, n = 5: total becomes 10 + 5 = 15. The loop ends and print(total) outputs the final value.

Answer: The program prints 15 — the sum of the numbers 1 to 5.

Common mistakes
  • Forgetting that range stops one short Correction: range(1, 6) gives 1 to 5 — always check whether your stop value is included (it never is).
  • Mixing up = and == Correction: = stores a value while == compares — writing if x = 5: is a syntax error in Python; conditions need the double equals, as in if x == 5:.

Practice

What does this print? Line 1: x = 3. Line 2: y = 4. Line 3: print(x + y * 2)
Remember BIDMAS: multiplication before addition.

11 — y * 2 is 8, plus x (3) gives 11.

What does this print? Line 1: for i in range(3): Line 2 (indented): print(i)
Where does range(3) start, and where does it stop?

0, then 1, then 2, each on its own line — range(3) starts at 0 and stops before 3.

What does this print? Line 1: name = 'Ada'. Line 2: print('Hello, ' + name)
The + joins two strings together.

Hello, Ada.

Fix the bug: if age = 16: print('old enough for a moped')
One symbol stores, two symbols compare.

if age == 16: — a single = is assignment and causes a syntax error here; == is the comparison you need.

Quick check

Output and Input — quick check

Which of these best defines "function"?

A named block of code that performs one task and can be called repeatedly, often with different inputs called parameters.

Write a function double(n) that returns twice its input, then state what double(7) returns.

def double(n): followed by an indented line return 2 * n — and double(7) returns 14.

What is the value of count after this loop? Line 1: count = 0. Line 2: while count is less than 3: Line 3 (indented): count = count + 1

3 — the loop runs while count is 0, 1 and 2, adding 1 each time, then stops when count reaches 3.
Key takeaways
  • Output and Input: make programs talk to the user with print and input.
  • Programs are input, process, output: Nearly every program follows the same shape: take something in, do something with it, and show a result.
  • sequence: Instructions carried out one after another in the order they are written — the default behaviour of every program.
  • Watch out for: forgetting that range stops one short