-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcard.py
More file actions
40 lines (30 loc) · 2 KB
/
card.py
File metadata and controls
40 lines (30 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# coding: utf8
"""
## Credit Card Verification
Credit cards numbers are verified using the Luhn formula. The formula verifies a number against
its included check digit, which is usually appended to a partial account number to generate
the full account number. This number must pass the following test:
1. From the **rightmost** digit, which is the check digit, and moving left, double the value
of every second digit. If the result of this doubling operation is greater than 9 (e.g., 8 × 2 = 16),
then add the digits of the product (e.g., 16: 1 + 6 = 7, 18: 1 + 8 = 9) or, alternatively, the same
result can be found by subtracting 9 from the product (e.g., 16: 16 − 9 = 7, 18: 18 − 9 = 9).
2. Take the sum of all the digits.
3. If the total modulo 10 is equal to 0 (if the total ends in zero) then the number is valid according to
the Luhn formula; else it is not valid.
Assume an example of a card number "79927398713":
|---------------------|----|----|----|----|----|----|----|----|----|----|----|
| Digit index | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---------------------|----|----|----|----|----|----|----|----|----|----|----|
| Card number | 7 | 9 | 9 | 2 | 7 | 3 | 9 | 8 | 7 | 1 | 3 |
| Double every second | 7 | 18 | 9 | 4 | 7 | 6 | 9 | 16 | 7 | 2 | 3 |
| Sum digits | 7 | 9 | 9 | 4 | 7 | 6 | 9 | 7 | 7 | 2 | 3 |
|---------------------|----|----|----|----|----|----|----|----|----|----|----|
The sum of all the digits in the third row is 70, which means the card number is valid (70 module 10 is 0).
Write a function `check_card(number)` that implements the following algorithm and returns `True` of `False`
indicating if the card number is correct.
Use it to verify your credit card number. You may also check the correctness of the following numbers:
`79927398710`, `79927398711`, `79927398712`, `79927398713`, `79927398714`, `79927398715`, `79927398716`,
`79927398717`, `79927398718`, `79927398719`.
"""
def check_card(number):
pass