0

from cs50 import get_int, get_string import sys

Prompt credit number input

cardnumber = get_int("Number : ")

#copying the credit card numbers into other variable / arrays cardnumber original_number = []

while cardnumber > 0 : x = cardnumber % 10 original_number.append(x) cardnumber = int(cardnumber / 10)

#calculating the length of the card number count = len(original_number)

#if the user enters the wrong card size if count != 13 and count != 15 and count != 16 : print("INVALID") sys.exit(1)

#copying the card number array into another array on which we can apply the law number = []

for i in range(count) : y = original_number[i] number.append(y)

#applying the law of the array for i in range(1,count,2) : number[i] = number[i] * 2

for i in range(count) : print(number[i],end="") print()

sum = 0 for i in number : sum = sum + i

#exiting the program if the sum is not divisible by 10 if sum%10 != 0 : print("INVALID") sys.exit(2)

#applying all the conditions to determine the card number if sum%10 == 0 : if count == 13 : if original_number[12] == 4 : print("VISA") sys.exit(0)

if count == 15 :
    if original_number[14] == 3 and (original_number[13] == 4 or original_number[13] == 7) :
        print("AMEX")
        sys.exit(0)

if count == 16 :
    if original_number[15] == 4 :
        print("VISA")
        sys.exit(0)

    elif original_number[15] == 5 and (original_number[14] >= 1 and original_number[14]<=5) :
        print("MASTERCARD")
        sys.exit(0)

else:
    sys.exit(3)

1 Answer 1

1

The Luhn's algorithm says you must add together the sum of the digits when multiplying by 2.

So if your card number digit is 8, for example, 8 * 2 is 16, so you need to add 1+6 to your sum, not 16.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .