- 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.
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.
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.
- 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
11 — y * 2 is 8, plus x (3) gives 11.
0, then 1, then 2, each on its own line — range(3) starts at 0 and stops before 3.
Hello, Ada.
if age == 16: — a single = is assignment and causes a syntax error here; == is the comparison you need.
Quick check
Which of these best defines "function"?
Write a function double(n) that returns twice its input, then state what double(7) returns.
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
- 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