Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

2.8 (2 of 2): The check digit

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

35 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

How does the machine know?

You type your card number into a website. You get one digit wrong. The site says the number is not valid before it asks the bank anything.

It did a quick sum with the digits. One digit of the number is there to make that sum work.

Key words

WordWhat it meansExample
Check digitAn extra digit worked out from the other digits. It is used to find mistakes.the last digit of an account number
AlgorithmA set of steps to follow to do a job.
RemainderWhat is left over after dividing.89 divided by 10 leaves 9
ModulusThe Python sign % that gives the remainder.89 % 10 is 9
ValidationA check that data is correct and makes sense.
DigitOne of the symbols 0 to 9.

The cells students fill in are left blank in this preview.

A check digit catches typing mistakes

A check digit is added to the end of a code, such as an account number. It is worked out from the other digits by an algorithm.

When somebody types the code, the computer works the digit out again. If the two digits are different, the code has a mistake. This is a kind of validation.

Real systems use many different algorithms. You do not need to learn any of them. If a question needs one, it gives you the steps.

Try it: the remainder

Code: python
print(89 % 10)
print(17 % 5)
print(20 % 10)
print(7 % 10)

# % gives what is left after dividing.
# 89 divided by 10 is 8 with 9 left over.
# Change the numbers and predict the answer first.

Make a free account to save your work and track your progress.

Worked example: one simple algorithm

StepCode 245Code 123
Add the digits2 + 4 + 5 = 111 + 2 + 3 = 6
How far to the next ten?20 - 11 = 910 - 6 = 4
The check digit is94
The full code is24591234
Check: add all four digits2 + 4 + 5 + 9 = 201 + 2 + 3 + 4 = 10

The cells students fill in are left blank in this preview.

No check finds every mistake

Take the code 1234. Somebody types 1324. The 2 and the 3 swapped places. The digits still add up to 10. This simple rule does not find that mistake.

Real systems use cleverer rules. The idea is the same: work the digit out again, then compare.

Try it: check a code

Code: python
code = "2459"
digits = code[:-1]
last = int(code[-1])
total = 0

for letter in digits:
    total = total + int(letter)

# The extra % 10 makes a remainder of 0 give a check digit of 0.
check = (10 - total % 10) % 10
print("Check digit should be:", check)

if check == last:
    print("The code is valid.")
else:
    print("The code has a mistake.")

# Change the last digit of the code and run it again.

Make a free account to save your work and track your progress.

When the remainder is 0

Warning

If the total already ends in 0, the remainder is 0. Then the check digit is 0, not 10. A check digit is one digit only.

Quick check: the arithmetic

Have a go

Quick check: the arithmetic

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Put the steps in order

Have a go

Put the steps in order

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Watch: a check digit worked out and checked

Press Play on the short film below. It works out the check digit for 245, one step at a time. Then it checks two typed codes. Watch which mistake the adding rule misses.

Film: the check digit

The check digit

Every scene, as text

  1. A check digit is worked out from the other digits. The lesson's rule adds them: for the code 245, the loop adds each digit in turn, and total goes 2, then 6, then 11.
  2. % is modulus, the remainder sign: 11 % 10 is 1. 9 more makes the next ten, 10 - 1, so the check digit is 9 and the full code is 2459. The last % 10 turns a 10 into 0, so a remainder of 0 gives a check digit of 0, not 10.
  3. Somebody types 2459. code[-1] picks out its last digit, 9. The check digit of 245 is 9 too, so it prints valid.
  4. One digit typed wrong: 2458. The check digit of 245 is still 9, but the last digit is 8. 9 is not 8, so else runs and it prints not valid: the mistake is caught.
  5. Myth (false): a check digit catches every mistake. 1234 typed as 1324, the 2 and the 3 swapped: the first three digits, 1, 3 and 2, still add up to 6, so the check digit is still 4 and it prints True: the swap is missed.
  6. Real systems use cleverer rules (Extra, not in the exam). A shop barcode gives each place a weight, 1, then 3, then 1, and adds each digit times its weight: 123 gives 10 and the swapped 132 gives 12. The totals differ, so the swap is usually found.

Checking a code as a flowchart

A flowchart from Start to End: Input the code, then "Is it all digits?", where No goes to Output: not valid and Yes to Work out the check digit from all but the last digit, then "Is it the same as the last digit?", where Yes goes to Output: valid and No to Output: not valid.A flowchart from Start to End: Input the code, then "Is it all digits?", where No goes to Output: not valid and Yes to Work out the check digit from all but the last digit, then "Is it the same as the last digit?", where Yes goes to Output: valid and No to Output: not valid.

Match the word to its meaning

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Your turn: check a code

Write two functions about check digits. simple_check_digit(digits) adds the digits and gives back the digit that makes the total end in 0. is_valid_code(code) is True only for a good code. It has at least 2 characters and they are all digits. The last digit is the check digit of the digits before it. Check the type first. Press Run Tests to check.

Code: python
def simple_check_digit(digits):
    # digits is text such as "245". Add up the digits (2 + 4 + 5 = 11).
    # Give back the check digit that makes the total end in 0. For 245 that is 9.
    # If the total already ends in 0, the check digit is 0.
    return None


def is_valid_code(code):
    # True if code is a good code: at least 2 characters, all digits,
    # and the last digit equals simple_check_digit() of all the digits before it.
    # For example "2459" is good and "2458" is not.
    return None

Make a free account to save your work, or press Start lab to work through the whole lesson.

Extra: A real rule: barcode weights

The adding rule misses a swap, such as 1234 typed as 1324. A shop barcode uses a better rule.

Each place gets a weight. A weight is a number you multiply a digit by. The weights go 1, then 3, then 1, then 3, and so on. Add up every digit times its weight. The check digit makes that total end in 0.

Now a swap of two digits usually changes the total, so the mistake is found.

Extra: Worked example: a real barcode

DigitWeightDigit times weight
414
030
010
6318
313
8324
111
339
313
339
919
339

The cells students fill in are left blank in this preview.

Extra: Add up the last column

Note

Add the last column: the total is 89. 89 divided by 10 leaves a remainder of 9. The check digit is 10 minus 9, which is 1.

Extra: Try it: a barcode check digit

Code: python
code = "400638133393"
total = 0
weight = 1

for letter in code:
    total = total + int(letter) * weight
    if weight == 1:
        weight = 3
    else:
        weight = 1

print("Total:", total)
# The extra % 10 makes a remainder of 0 give a check digit of 0.
print("Check digit:", (10 - total % 10) % 10)

# Try the code "590123412345". The check digit is 7.

Make a free account to save your work and track your progress.

Extra: Your turn: check a real barcode

Extra, not in the exam. Write two functions for a real 13 digit shop barcode. check_digit(first12) uses the weights 1, 3, 1, 3 and gives back a whole number. is_valid_barcode(code) is True only for a 13 digit code whose last digit is right. Check the length and the type first. Press Run Tests to check.

Code: python
def check_digit(first12):
    # first12 is 12 digits as text. The weights go 1, 3, 1, 3 and so on from the left.
    # Add up each digit times its weight. Give back the digit that makes the total end in 0.
    # Give it back as a whole number.
    return None


def is_valid_barcode(code):
    # True if code is a good 13 digit barcode.
    # It must be exactly 13 characters, all digits, and the last digit
    # must equal check_digit() of the first 12.
    return None

Make a free account to save your work, or press Start lab to work through the whole lesson.

Key words crossword

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.