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
| Word | What it means | Example |
|---|---|---|
| Check digit | An extra digit worked out from the other digits. It is used to find mistakes. | the last digit of an account number |
| Algorithm | A set of steps to follow to do a job. | |
| Remainder | What is left over after dividing. | 89 divided by 10 leaves 9 |
| Modulus | The Python sign % that gives the remainder. | 89 % 10 is 9 |
| Validation | A check that data is correct and makes sense. | |
| Digit | One 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
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
| Step | Code 245 | Code 123 |
|---|---|---|
| Add the digits | 2 + 4 + 5 = 11 | 1 + 2 + 3 = 6 |
| How far to the next ten? | 20 - 11 = 9 | 10 - 6 = 4 |
| The check digit is | 9 | 4 |
| The full code is | 2459 | 1234 |
| Check: add all four digits | 2 + 4 + 5 + 9 = 20 | 1 + 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 = "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 questionsPut the steps in order
Have a go
Put the steps in order
3 questionsWatch: 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
- 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.
- % 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.
- Somebody types 2459. code[-1] picks out its last digit, 9. The check digit of 245 is 9 too, so it prints valid.
- 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.
- 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.
- 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
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.
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
| Digit | Weight | Digit times weight |
|---|---|---|
| 4 | 1 | 4 |
| 0 | 3 | 0 |
| 0 | 1 | 0 |
| 6 | 3 | 18 |
| 3 | 1 | 3 |
| 8 | 3 | 24 |
| 1 | 1 | 1 |
| 3 | 3 | 9 |
| 3 | 1 | 3 |
| 3 | 3 | 9 |
| 9 | 1 | 9 |
| 3 | 3 | 9 |
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 = "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.
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