The question was Use Luhn's Algorithm to check if a credit card number is valid or not. If it is valid then print whether the card is VISA, MASTERCARD or AMERICAN EXPRESS. (Go to your preferred search engine for any reference regarding Luhn's Algorithm or anything else) Sample Test Case : Input: 4003600000000014
and the code below for it ->
n= str(n)
a=0
for i in range(0,len(n)):
if i%2==0:
a=a+int(n[i])
else:
x=int(n[i])*2
x=str(x)
for i in range(0,2):
a=a+int(i)
b= n+ str(a)
b= int(n)
return b
n= input("Enter your credit card number (without spaces) :")
x= luhn(n)
l= len(n)
if str(x)==str(n):
if (int(n[0:2])==34 or 37) and (l==15):
print("American Express")
elif (int(n[0:2])==51 or 55) and (l==16):
print("MasterCard")
elif (int(n[0:2])==36 or 38) or (int(n[0:3])==300 or 305) and (l==14):
print("Diners Club and Carte Blanche")
elif (int(n[0])==4) and (l==13 or 16):
print("Visa")
elif (int(n[0:4])==6011) and (l==16):
print("Discover")
elif (l==15) and (int(n[0:4])==2123 or 1800):
print("JCB")
elif (l==16) and (int(n[0])==3):
print("JCB")
else:
print("Invalid Card")
else:
print("Invalid Card")
I clearly defined the condition for each card but for the test case given above, it is giving the output MASTERCARD and I am not able to understand why, The condition for Mastercard is nowhere close to the condition for Visa, it doesnt even start with 4, yet its giving the output Mastercard (the output for the function is correct, checked it already)
My source for the algorithm in case anyone asks- https://www.ibm.com/docs/en/order-management-sw/9.3.0?topic=cpms-handling-credit-cards