Skip to content

Instantly share code, notes, and snippets.

@Allwin12
Created December 8, 2020 17:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Allwin12/42e10cc21e66b1ff764a18058c74a244 to your computer and use it in GitHub Desktop.
Save Allwin12/42e10cc21e66b1ff764a18058c74a244 to your computer and use it in GitHub Desktop.
def luhn_checksum(card_number):
def digits_of(n):
return [int(d) for d in str(n)]
digits = digits_of(card_number)
odd_digits = digits[-1::-2]
even_digits = digits[-2::-2]
checksum = 0
checksum += sum(odd_digits)
for d in even_digits:
checksum += sum(digits_of(d*2))
return checksum % 10
print('Valid') if luhn_checksum("4532015112830366")==0 else print('Invalid')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment