I'm very much new to programming. I implemented Credit card validation program according to following instructions.
- Let the input be input.
- Reverse input.
- Multiply all odd positions (i.e. indexes 1, 3, 5, etc.) of input by 2. If any of these multiplied entries is greater than 9, subtract 9.
- Sum all of the entries of input, store as sum.
- If sum (mod 10) is equal to 0, the credit card number is valid.
code
# credit card validation - Luhn Formula
card_number = list(reversed(input("enter card number: ")))
status = False
temp1 = []
temp2 = []
sum = 0
for i in card_number:
if card_number.index(i) % 2 != 0:
temp1.append(int(i) * 2)
else:
temp1.append(int(i))
for e in temp1:
if e > 9:
temp2.append(e - 9)
else:
temp2.append(e)
for f in temp2:
sum += f
if sum % 10 == 0:
status = True
print("card is VALID")
else:
print("card is INVALID")
code sometimes works and sometimes not. is there a problem with my code.
valid number 49927398716 - works
valid number 4916092180934319 - not working
please don't link to this - Implementation of Luhn Formula I don't need another implementation. If possible please tell me what's wrong with my code. So I can correct it.
Thank you