DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Credit Card Checksum

09.16.2005
| 3257 views |
  • submit to reddit
        From David Shaw's <a href=http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/172845>recipe</a>
def cardLuhnChecksumIsValid(card_number):
    sum = 0
    num_digits = len(card_number)
    oddeven = num_digits & 1
    for count in range(0, num_digits):
        digit = int(card_number[count])
        if not (( count & 1 ) ^ oddeven ):
            digit = digit * 2
        if digit > 9:
            digit = digit - 9
        sum = sum + digit
    return ( (sum % 10) == 0 )