Checks if a credit card number is valid based off of the techniques from this infographic.
Created
August 5, 2012 23:59
-
-
Save onkursen/3268191 to your computer and use it in GitHub Desktop.
Credit Card Validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
card = raw_input("Enter credit card number: ") | |
num = map(int, card.strip()) | |
total = 0 | |
for i in range(len(num)): | |
if i % 2 == 0: | |
con = num[i]*2 | |
total += con if con < 10 else 1 + (con % 10) | |
else: | |
total += num[i] | |
if total % 10 == 0: | |
print "Valid credit card" | |
else: | |
print "Invalid credit card; watch out!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! Just something I wanted to code while going through old pictures on my computer. I also wanted to save it somewhere, and I figured a Gist would be a good place for it.