0

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()

1 Answer 1

1

When you are consuming the cardNo = cardNo//10 you should be dividing by 100 rather than 10 so that you consume the last 2 digits each time. Otherwise, you will end up double-counting some of the digits.

You are correctly doing that with creditNo.

Once you get past that, you'll run into some other issues but perhaps you can figure those out.

3
  • Thanks for your help, I understood where I went wrong. My code is now able to correctly recognise AMEX card numbers but not Master or VISA. What mistake in my code may have caused this?
    – Prav Elan
    Commented Jun 4, 2018 at 5:43
  • @PravElan if firstTwoDigits == 34 or 37: this does not do what you expect.
    – curiouskiwi
    Commented Jun 6, 2018 at 0:30
  • Oh! I think I got it. Thanks a lot :)
    – Prav Elan
    Commented Jun 10, 2018 at 1:31

You must log in to answer this question.

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