In [186]:
def credit():
print "Please enter Visa or Amex credit card: "
card=raw_input()
card_rev=card[::-1]
total = 0
for i in card_rev[1::2]:
#print "Printing i %s" %i
x= int(i)*2
if len(str(x)) == 2:
for a in str(x):
#print "Printing a %s" %a
total += int(a)
#print "Printing subtotal %s" %total
else:
total +=int(x)
#print "Printing total %s" %total
#print total
for i in card_rev[::2]:
total += int(i)
#print total
#print total
if ((len(card) == 16 or len(card) == 13 ) and int(card[0]) == 4 and (total % 10 == 0 )) or \
(len(card) == 15 and (int(card[:2]) == 34 or int(card[:2]) == 37) and (total % 10 == 0 )):
print "This is a valid Credit Card"
else:
print "Invalid credit card"
In [187]:
credit()
In [126]:
test='4111111111111111'
In [139]:
test[::-1][1::2]
Out[139]:
In [183]:
test_2='371449635398431'
In [162]:
test_2[::-1][::2]
Out[162]:
In [167]:
test_2[::-1][1::2]
Out[167]:
In [ ]: