Advertisement
acclivity

pyCreditCardChecker

Dec 23rd, 2021 (edited)
1,831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. # Credit Card Number Check using Luhn's Algorithm
  2.  
  3. # Starting from the right end, add up the sum of the digits, but for every other digit, double it before adding.
  4. # Where the doubled digit results in a 2-digit number, use the sum of those two digits.
  5.  
  6. # The resulting sum should be a multiple of 10
  7.  
  8. def cc_check(cnum):
  9.     cnum1n = [int(c) for c in cnum[::-2]]
  10.     cnum2n = [2 * int(c) for c in cnum[:-1][::-2]]
  11.     cnum3 = [n if n < 10 else n - 9 for n in cnum2n]
  12.     return not (sum(cnum1n) + sum(cnum3)) % 10
  13.  
  14. cc = "4417123456789113"
  15. print(cc, cc_check(cc))
  16. # 4417123456789113 True
  17.  
  18. cc = "4417123456789114"
  19. print(cc, cc_check(cc))
  20. # 4417123456789113 False
  21.  
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement