This is my approach, didn't take too much time but also wasn't tested thoroughly
## function to validate credit card number using Luhn's algorithm, the input must be in a list format"##
def card_validate(number):
sum = 0
for i in range(0, len(number)):
if i%2 == 0:
sum += number[i]
else:
sum += (number[i]%10 + number[i]//10)
if sum % 10 == 0:
return True
else:
return False
1 answers ( 0 marked as helpful)
I noticed that I forgot to multiply 2nd number by 2, so I corrected the code:
def card_validate(number):
sum = 0
for i in range(0, len(number)):
if i%2 == 0:
sum += number[i]
else:
n2 = 2* number[i]
sum += (n2%10 + n2//10)
if sum % 10 == 0:
return True
else:
return False