Learn practical skills, build real-world projects, and advance your career

Background Information - Valid credit card numbers

What makes a credit card number valid? For our purposes, we'll assert that a number is valid if it passes the Luhn algorithm. (In practice there are also additional restrictions. For example, all Visa cards must start with the number 4)

An algorithm is a set of instructions for accomplishing a specific task, so the Luhn algorithm is a set of instructions for checking whether a number is potentially a valid credit card number. You can read about the algorithm in this article.

To see how the Luhn algorithm works, let us use it to validate the number 9813428854407:

  1. Double the value of alternate digits of the credit card number beginning with the second digit from the right (the first right-hand digit is the check digit).
    9 16 1 6 4 4 8 16 5 8 4 0 7
  2. Add the individual digits comprising the products obtained in Step 1 to each of the unaffected digits in the original number.
    9 + (1+6) + 1 + 6 + 4 + 4 + 8 + (1+6) + 5 + 8 + 4 + 0 + 7 = 70
  3. If the total obtained in Step 2 is a number ending in zero (30, 40, 50, etc.), then the account number is valid. 70 ends in a 0, so the account number is valid.

Make sure you understand it well enough to answer the following questions:

  • Is 1234567890123 a valid number?
  • If the first 12 digits of a 13-digit credit card number are 111111111111, what must the 13th digit be?

Specifications

Your program will ask the user for a 6 digit number, then generate three valid 13 digit credit card numbers each starting with those initial 6 digits.

Your program must include the following functions. In other words, in your code there must be functions that have the same exact names, that take the arguments described, and that return the values described. You may also choose to define additional functions, if you'd like.

  1. Verify this function takes a 13 digit integer as input and returns True if the number passes the Luhn algorithm amd False if it fails.
  2. Generate this function takes a 6 digit integer and returns a valid 13 digit credit card number which begins with the given 6 digits. The return value must be of type int.
    It is fine if your code only works if the user enters a 6 digit number (in other words, the initial number cannot start with a 0). But the 6 randomly generated digits must be selected from the full range from 0 through 9.
  3. Main this function asks the user for an initial 6 digit number, then generates and prints three valid credit card numbers that start with those 6 numbers. See example run below.

Example Run


Enter a 6 digit number:
981342
Three valid numbers:
9813428854407
9813424039581
9813420046028
def Luhn(num = -1, ifShow = False):
    if (num == -1):
        num = input("Enter number: ")
    else:
        num = str(num)
    list = []
    index = len(num) - 1 
    
    for x in num:
        if ( (len(num) - 1 - index) % 2 == 1 ):
            tempNum = int(num[index]) * 2
            tempNum2 = tempNum
            if (tempNum >= 10):
                tempNum2 = (tempNum // 10)
                tempNum2 += (tempNum % 10)
            list.append(tempNum2)
        else:
            list.append(int(num[index]))
        index -= 1
    
    sumOfList = str(sum(list))
    ifValid = (sumOfList[len(sumOfList) - 1:len(sumOfList)] == '0')
    
    if (ifShow == True):
        print (sumOfList)
    
    return (ifValid)
Luhn(1234567890123)
False
Luhn(9813428854407)
True
Luhn(9813424039581)
True