Skip to content

Instantly share code, notes, and snippets.

@onkursen
Created August 5, 2012 23:59
Show Gist options
  • Save onkursen/3268191 to your computer and use it in GitHub Desktop.
Save onkursen/3268191 to your computer and use it in GitHub Desktop.
Credit Card Validation

Credit card validation

Checks if a credit card number is valid based off of the techniques from this infographic.

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!"
@aaronroe
Copy link

aaronroe commented Aug 6, 2012

very nice.

@onkursen
Copy link
Author

onkursen commented Aug 6, 2012

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment