Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Luhn Formula
#1
Hi everyone. I very much new to programming.
I implemented Credit card validation program according to following instructions.

1. Let the input be input.
2. Reverse input.
3a. Multiply all odd positions (i.e. indexes 1, 3, 5, etc.) of input by 2.
3b. If any of these multiplied entries is greater than 9, subtract 9.
4. Sum all of the entries of input, store as sum.
5. 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.
Thank you
Reply


Messages In This Thread
Luhn Formula - by vdv34czx - May-17-2018, 05:30 AM
RE: Luhn Formula - by ThiefOfTime - May-17-2018, 06:53 AM
RE: Luhn Formula - by buran - May-17-2018, 07:30 AM
RE: Luhn Formula - by buran - May-17-2018, 08:05 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020