AQA GCSE CSPaper 1 · Unit 2Lesson 9

Paper 1 · Unit 2 · CS-L2-09

Input, Output & Random Numbers

60 minutes · AQA 8525 · Paper 1 — Programming

Spec & Goals 3 min

AQA Spec 3.2.7 & 3.2.9 · Input/output and random number generation

By the end of this lesson you can:

  1. Read a value from the user with input and display a value with output.
  2. Write a clear prompt so the user knows what to type.
  3. Use random number generation with RANDOM_INT(low, high) to pick a whole number in a range.

Warm-Up 5 min

Last lesson you grouped related fields into a record. Today the program talks to the user and rolls a dice.

Quick starter

Aisyah types her name into a quiz game. The game then prints a welcome message.

Which step reads data, and which step displays data?

Reveal the answer

Reading her name is input. Printing the welcome message is output. Every interactive program does both.

Key Concept — talking to the user 14 min

Input reads data from the user into the program. Output displays data from the program to the user.

Always give a prompt

A prompt is a short message that tells the user what to type. Without one, the user just sees a blank line.

Pseudo-codeWhat it means
OUTPUT 'Enter your name'Display a prompt to the user.
name ← USERINPUTRead what the user types into name.
OUTPUT nameDisplay the value stored in name.

Random number generation

Sometimes a program needs an unpredictable value — a dice roll, a card, a random question.

Random numbers are used in games (dice, spinners), simulations and picking a random item from a list.

Worked Example — a dice roller and a greeter 12 min

Problem A: roll a six-sided dice and display the result.

AQA pseudo-code

roll ← RANDOM_INT(1, 6)
OUTPUT 'You rolled a ' + roll

The same in Python

import random

roll = random.randint(1, 6)
print("You rolled a", roll)

Problem B: ask the user for their name, then greet them.

AQA pseudo-code

OUTPUT 'Enter your name'
name ← USERINPUT
OUTPUT 'Hello, ' + name

The same in Python

name = input("Enter your name: ")
print("Hello,", name)

Try It Yourself 12 min

🟢 Easy

Goal: Write a line of AQA pseudo-code that outputs the message 'Game over'.

Hint: use OUTPUT followed by the text in quotes.

🟡 Medium

Goal: Write pseudo-code that prompts for the user's favourite number, reads it, then outputs it back.

Hint: OUTPUT a prompt, then ← USERINPUT, then OUTPUT the variable.

🔴 Stretch

Goal: Write pseudo-code that simulates rolling two dice and outputs their total. Use RANDOM_INT(1, 6) twice.

📝 Exam Practice 10 min

Answer the way the examiner expects — the command word and the marks tell you how much to write.

Write[1 mark]

Write a line of pseudo-code that outputs the value of score.

Mark scheme
  • OUTPUT score (1).
State[1 mark]

State what RANDOM_INT(1, 6) returns.

Mark scheme
  • A random whole number from 1 to 6 inclusive (1).
Give[1 mark]

Give one type of program that would use random numbers.

Mark scheme
  • Any one (1): a dice / board game · a card game · a simulation · a quiz that picks a random question · a lottery / spinner.
State[1 mark]

Look at the greeter pseudo-code. State the purpose of the first line, OUTPUT 'Enter your name'.

Mark scheme
  • It is a prompt / it tells the user what to type / what input is expected (1).

Recap & Key Terms 3 min

Programs talk to the user with input (USERINPUT) and output (OUTPUT). A good prompt tells the user what to type. RANDOM_INT(low, high) returns a random whole number in a range, both ends included.

Input
Data read into a program from the user, e.g. x ← USERINPUT.
Output
Data displayed by a program to the user, e.g. OUTPUT x.
Prompt
A message that tells the user what input is expected.
Random number generation
Producing an unpredictable value; RANDOM_INT(low, high) returns a random whole number from low to high inclusive.

Homework 1 min

Task (≤ 15 min): Write, in AQA pseudo-code, a program that asks the user for their name, picks a random "lucky number" from 1 to 100, and outputs a message containing both.

Model answer
OUTPUT 'Enter your name'
name ← USERINPUT
lucky ← RANDOM_INT(1, 100)
OUTPUT 'Hello ' + name + ', your lucky number is ' + lucky

Award marks for: a prompt + USERINPUT (1), RANDOM_INT(1, 100) (1), output combining the name and the number (1).