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
#2
The main problem that I see is that you are searching for the index of the element in the list, but since a card number can contain some digtits more than once a card number could look like this: 1337
if you perform card_number.index("3") it will return 1 in the first iteration, since the first 3 is on index 1, but on the next turn it will return 1 again, though you are searching for a different three, the first three that the program finds is on index one.
If I may, I would like to give you some feedback to your code:
  1. since you are not using the list card_number again, consider working on that list to reduce memory space.
  2. also you are using too many loops (at least 2). Loops in python aren't efficient in any regard so reducing the amount of loops you need speeds up your program
  3. use the already implemented sum function ;)
if you want to use loops I would suggest to do it that way:
for i, card_digit in enumerate(card_number):
      if i % 2 == 0:
           new_digit = int(card_digit) * 2
           if new_digit > 9:
                  new_digit -= 9
           card_number[i] = new_digit
      else:
            card_number[i] = int(new_digit)
digit_sum = sum(card_number)
if digit_sum % 1 == 0:
       status = True
       print("card is VALID")
else:
       print("card is INVALID")
also try to avoid using the name of defined functions (like sum) as variable names ;)
Even that one loop I used could be avoided, by using numpy :)

But as I said, your main problem was the index. By using enumerate you get the value at index i and also the index i. So that there is no need to search for the index :)
Reply
#3
for i in card_number:
    if card_number.index(i) % 2 != 0:
        temp1.append(int(i) * 2)
    else:
        temp1.append(int(i))
Here you iterate over digits in card_number. So i is digit in the card number and card_number.index(i) is the index of the first occurrence of i in card_number. Use enumerate to get also the index

card_number = list(reversed(input("enter card number: ")))
digits = []

for i, digit in enumerate(card_number):
    if i % 2:
        d = int(i) * 2
        digits.append(d - 9 if d > 9 else d)
    else:
        digits.append(int(i))
 
if not sum(digits) % 10:
    print("Card is VALID")
else:
    print("Card is INVALID")
Note that you should not use built-in function names like sum as variable names.
To improve the code further you may want to make a function, that checks the card number it gets as arguments and returns True or False. This way you will divide your code in small pieces of building blocks

EDIT: fixed an error in my code
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
@volcano63 noted that it will work even without explicit conversion to list here card_number = list(reversed(input("enter card number: ")))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

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