Luhns Algorithm, mod 10 check

1.2k views Asked by At

I have to make an assignment for my course computational thinking, but when I give this valid credit card number as input it keeps saying that the number is invalid, does anyone know why??

def ask_user():
string = (str(input("Please input your credit card number")))
numbers = list(map(int, string.split()))
if card_length(numbers):
    validation(numbers)
else:
    print("The credit card number you entered is invalid")

""" This function takes the credit card number as a parameter and checks the length of the credit card number to see if it is valid or not. """

def card_length(numbers):
for i in numbers:
    if 13 <= i <= 16:
        if numbers[0] == 4 or numbers[0] == 5 or numbers[0] == 6 or (numbers[0] == 3 and numbers[1] == 7):
            return True
else:
    return False

""" This method takes the list of numbers and tells the user if it is a valid combination with a print statement """

def validation(numbers):
odd_results = odd_digits(numbers)
even_results = even_digits(numbers)
sum_of_results = odd_results + even_results
if sum_of_results % 10 == 0:
    print("This credit card number is valid")
else:
    print("This credit card number is invalid")

""" This function takes the credit card number as a string list parameter and adds all of the even digits in it. """

def even_digits(numbers):
length = len(numbers)
sumEven = 0
for i in range(length - 2, -1, -2):
    num = eval(numbers[i])
    num = num * 2
    if num > 9:
        strNum = str(num)
        num = eval(strNum[0] + strNum[1])
        sumEven += num
    return sumEven

""" This function takes the credit card number as a string list parameter and adds all of the odd digits in it. """

def odd_digits(numbers):
length = len(numbers)
sumOdd = 0
for i in range(length - 1, -1, -2):
    numbers += sumOdd
return sumOdd

""" This is the main function that defines our first function called ask_user """

 def main():
 ask_user()



if __name__ == '__main__':
main()
1

There are 1 answers

0
Mohammed Mustufa On

The split() method returns a list of strings after breaking the given string by the specified separator.

Syntax : str.split(separator, maxsplit)

  • separator : This is a delimiter. The string splits at this specified separator. If is not provided then any white space is a separator.
  • maxsplit : It is a number, which tells us to split the string into maximum of provided number of times. If it is not provided then there is no limit.

Since you are taking the credit card number as a string without any spaces between the numbers, on splitting the credit card number you will get a list which contains the entire credit card number as a single element of the list(numbers). Therefore, the length of the numbers list will always be one and the card length function will always return False. I suggest you do something like:

    string = (str(input("Please input your credit card number")))
    numbers = list(map(int, list(string)))

Moving on, let me explain how the Luhn algorithm works:

The last digit in the number is taken as checksum. Alternate numbers starting from the number next(on the left) to the checksum digit are doubled. If the number becomes a double digit on doubling then the individual digits are added: 14 --> 1+4 = 5 (or) we could also subtract 9 from the digit which would give us the same answer: 14-9 =5. All the digits(excluding the checksum digit) are now added. The sum of these digits is multiplied by 9 and the mod 10 of the number is taken. If the result of the mod 10 of the number is equal to the checksum digit, then the number is valid.

This is one of the ways in which you could write the code for the algorithm:

    card_number = list(map(int, list(input('Enter the credit card number : '))))
    card_number.reverse()
    checksum = card_number.pop(0)

    for i in range(len(card_number)):
        if i % 2 == 0:
            x = card_number[i] * 2
            if len(str(x)) == 2:
                x -= 9
            card_number[i] = x

    total = sum(card_number) * 9
    total %= 10

    if total == checksum:
        print('It is a valid number')
    else:
        print('It is not a valid number')