# Python：从循环创建两个列表并将两者相加

def main():

while True :
cardnumber = input("Please enter a credit card number: ")
if cardnumber.isdecimal() and int(cardnumber) > 0 :
break

count = len(cardnumber)

if count != 13 and count != 15 and count != 16:
print("INVALID")
else:
check(count, cardnumber)

def check(length, number):

lenght_max = 15

if length == 15 and int(number[0]) == 3 and (int(number[1]) == 4 or int(number[1]) == 7):
if validator(number):
print("AMEX")
elif length == 16 and int(number[0]) == 5 and int(number[1]) <= 5:
if validator(number):
print("MASTERCARD")
elif length == 16 or length == 13 and int(number[0]) == 4:
if validator(number):
print("VISA")
else:
print("INVALID")
return number

def validator(num):

sum = 0
while num > 0:
sum += num % 10
num = num // 10

return sum
odd = [int(num[i]) * 2 for i in range(1, len(num), 2)]
even = [int(num[i]) for i in range(0, len(num), 2)]

new_sum = sum(validator(x) for x in odd) + sum(even)
if(new_sum % 10 == 0):
return True
else:
print("INVALID")

main()

cs95

def dig_sum(num):
sum = 0
while num > 0:
sum += num % 10
num = num // 10

return sum

num = '378282246310005' # your credit card number
odd = [int(num[i]) * 2 for i in range(1, len(num), 2)] # these two remain the same
even = [int(num[i]) for i in range(0, len(num), 2)]

new_sum = sum(dig_sum(x) for x in odd) + sum(even)
if(new_sum % 10 == 0):
print('Valid') #valid!

sum(dig_sum(x) for x in odd)将获得odd列表中每个数字的数字总和，sum(...)找到结果总和。

'378282246310005'

Valid

0条评论