SoFunction
Updated on 2024-11-13

python simulation loan card number generation rule process analysis

preamble

In the process of testing a web system, I need to use the "loan card number", and this loan card number can only be used once, after saving the next time you can not use the same card number again.

So I decided to write a piece of code to implement it myself based on its generation rules.

Also for convenience, the first three digits of the loan card are realized in numbers by default.

1. The rules of generation are as follows:

The loan card code has a total of 16 bits, with the last two being the check digits

The rules for coding the entire loan card are as follows:

The first three digits are either numbers or capital letters.

Fourth to fourteenth digits: numbers, respectively

The checksum for the last two digits is

the number obtained by multiplying the first fourteen digits by the sum of the weights and dividing by the remainder of 97 and then adding 1.

If the number is a single-digit number, it needs to be preceded by a zero.

The first fourteen codes correspond to weights 1,3,5,7,11,2,13,1,1,17,19,97,23,29

If a bit is a letter, you need to convert this letter to a number, A to 10, B to 11, and so on.

2. The python code is as follows:

import random

power = [1,3,5,7,11,2,13,1,1,17,19,97,23,29] #Weight
arr = []
sum = 0

# Randomly generate the first 14 digits and save them in the list arr
for i in range(14):
  value = (0,9)
  (value)

# The first fourteen digits multiplied by the weights add up
for j in range(14):
  value = arr[j] * power[j]
  sum = sum + value

#Divide by 97 and add 1 to the remainder.
last_two = sum % 97 + 1

# If the number is a single digit, it needs to be preceded by a zero.
if last_two>10:
  shiwei = last_two // 10
  gewei = last_two % 10
  (shiwei)
  (gewei)
else:
  shiwei = 0
  gewei = last_two
  (shiwei)
  (gewei)

# Output loan card number
print("loanCardNo: ",end="")
for i in range(0,16):
  print(arr[i],end="")

This is the whole content of this article.