Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Credit Card Number Check using Luhn's Algorithm
- # Starting from the right end, add up the sum of the digits, but for every other digit, double it before adding.
- # Where the doubled digit results in a 2-digit number, use the sum of those two digits.
- # The resulting sum should be a multiple of 10
- def cc_check(cnum):
- cnum1n = [int(c) for c in cnum[::-2]]
- cnum2n = [2 * int(c) for c in cnum[:-1][::-2]]
- cnum3 = [n if n < 10 else n - 9 for n in cnum2n]
- return not (sum(cnum1n) + sum(cnum3)) % 10
- cc = "4417123456789113"
- print(cc, cc_check(cc))
- # 4417123456789113 True
- cc = "4417123456789114"
- print(cc, cc_check(cc))
- # 4417123456789113 False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement