Jovian
⭐️
Sign In

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
In [1]:
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)
In [2]:
Luhn(1234567890123)
Out[2]:
False
In [3]:
Luhn(9813428854407)
Out[3]:
True
In [111]:
Luhn(9813424039581)
Out[111]:
True
In [112]:
Luhn(111111111111, True)
18
Out[112]:
False
In [113]:
Luhn(1111111111112, True)
20
Out[113]:
True

Answer to:

Is 1234567890123 a valid number?

False

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

The 13th digit be: 2

In [1]:
2+2
Out[1]:
4
In [5]:
import jovian as jvn
In [6]:
jvn.commit(secret = True)
[jovian] Saving notebook..
[jovian] Updating notebook "12b3c62d79424dcbad17af79921cf30a" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/bendesu/12b3c62d79424dcbad17af79921cf30a
In [7]:
jvn.commit(nb_filename="123")
[jovian] Saving notebook..
[jovian] Updating notebook "12b3c62d79424dcbad17af79921cf30a" on https://jovian.ml/
--------------------------------------------------------------------------- FileNotFoundError Traceback (most recent call last) <ipython-input-7-1869f350de52> in <module> ----> 1 jvn.commit(nb_filename="123") //miniconda3/envs/jovian-py-dev/lib/python3.5/site-packages/jovian/__init__.py in commit(secret, nb_filename, files, capture_env, env_type, notebook_id, create_new, artifacts) 121 122 # Upload the notebook & create/update the gist --> 123 res = create_gist_simple(nb_filename, notebook_id, secret) 124 if res is None: 125 return //miniconda3/envs/jovian-py-dev/lib/python3.5/site-packages/jovian/utils/api.py in create_gist_simple(filename, gist_slug, secret) 57 auth_headers = _h() 58 ---> 59 with open(filename, 'rb') as f: 60 nb_file = (filename, f) 61 log('Uploading notebook..') FileNotFoundError: [Errno 2] No such file or directory: '123'
In [ ]: