When I input a valid credit card number, my code returns INVALID. I'm not sure about where did I go wrong in my code despite analysing it for hours. I would appreciate any help with this. Thank you :)
My code is as follows:
#Credit.py
#Program to determine if credit card is legit
from cs50 import get_int
def main():
#Getting number from user
cardNo = get_int("What is the credit card number?\n")
creditNo = ccNo = cardNo
#Initialise temp variable
temp=0
#second-to-last digit onwards
while cardNo>0:
#Floor Division
multiplyDigit = (cardNo//10)%10
multiplyDigit *= 2
if multiplyDigit >= 10:
lastDigit = multiplyDigit%10
#firstDigit must be 1 because multiplyDigit max value is 9 (9*2=18)
firstDigit = 1
multiplyDigit = firstDigit + lastDigit
temp += multiplyDigit
multiplySum = temp
cardNo = cardNo//10
#Re-initialise temp variable
temp=0
#last digit onwards
while creditNo>0:
noMultiplyDigit = creditNo%10
temp += noMultiplyDigit
noMultiplySum = temp
creditNo = (creditNo//10)//10
#Adding both sums together
totalSum = multiplySum + noMultiplySum
#Getting last digit of totalSum
totalSum %= 10
#Ensure that credit card is VALID
if totalSum == 0:
#Checking if card number is a valid type
#Check the first two digits OR first digit
firstTwoDigits = int(str(ccNo[0:2]))
firstDigit = int(str(ccNo[0:1]))
#Verify if American Express
if firstTwoDigits == 34 or 37:
if len(str(ccNo)) is not 15:
print("INVALID")
return None
else:
print("AMEX")
return 0
#Verify if MasterCard
elif firstTwoDigits == 51 or 52 or 53 or 54 or 55:
if len(str(ccNo)) is not 16:
print("INVALID")
return None
else:
print("MASTER")
return 0
#Verify if Visa
elif firstDigit == 4:
if len(str(ccNo)) is not 13 or 16:
print("INVALID")
return None
else:
print("VISA")
return 0
else:
print("INVALID")
return None
else:
print("INVALID")
return None
if name == "main": main()