La guía o código que hallarás en este artículo es la solución más rápida y efectiva que encontramos a tus dudas o dilema.
Ejemplo 1: Python del algoritmo de Luhn
#Worse
#This is the longer Luhn's algorithm with step-by-step instructions
#Function to split a string into a list of all its constituent characters
def split_char(number_string):
return [char for char in number_string]
#Function to convert a string list to an integer list
def list_str_to_int(test_list):
for i in range(0, len(test_list)):
test_list[i] = int(test_list[i])
return test_list
#Function to convert an integer list to a string list
def list_int_to_str(test_list):
for i in range(0, len(test_list)):
test_list[i] = str(test_list[i])
return test_list
#Function to multiply all elements of a list by 2
def list_multiplied_by_2(test_list):
for i in range(0, len(test_list)):
test_list[i] *= 2
return test_list
#Prompt the user for Input
ccn = int(input("Enter your digital card number: "))
ccn = str(ccn)
ccn_string = str(ccn)
length_var = len(ccn)
#Specifying a condition to exit the loop
if 12 > length_var < 17:
print("INVALID")
exit()
# Condition for digital card number with even length
elif length_var % 2 == 0:
even_num1 = ccn
even_num2 = " " + ccn
even_num3 = "".join(even_num1[::2])
even_num4 = "".join(even_num2[::2])
even_alternate1 = even_num3.replace(" ", "")
even_alternate2 = even_num4.replace(" ", "")
even_sep1 = split_char(even_alternate1)
even_sep2 = split_char(even_alternate2)
even_str_to_int1 = list_str_to_int(even_sep1)
even_str_to_int2 = list_str_to_int(even_sep2)
even_str_2_1 = list_multiplied_by_2(even_str_to_int1)
even_int_to_str1 = list_int_to_str(even_str_2_1)
even_sep_digits1 = ""
for i in even_int_to_str1:
even_sep_digits1 += "".join(i)
even_sep3 = split_char(even_sep_digits1)
even_str_to_int3 = list_str_to_int(even_sep3)
even_sum_last_add1 = sum(even_str_to_int3)
even_sum_last_add2 = sum(even_str_to_int2)
even_checksum = even_sum_last_add1 + even_sum_last_add2
#Printing the type of digital card based on user input
if even_checksum % 10 == 0:
if len(ccn_string) == 14 or 16 and ccn_string[:1] == "4":
print("VISA")
elif len(ccn_string) == 16 and ccn_string[:2] == "51" or "52" or "53" or "54" or "55":
print("MASTERCARD")
else:
print("INVALID")
else:
print("INVALID")
# Condition for digital card number with odd length
elif length_var % 2 == 1:
odd_num1 = " " + ccn
odd_num2 = ccn
odd_num3 = "".join(odd_num1[::2])
odd_num4 = "".join(odd_num2[::2])
odd_alternate1 = odd_num3.replace(" ", "")
odd_alternate2 = odd_num4.replace(" ", "")
odd_sep1 = split_char(odd_alternate1)
odd_sep2 = split_char(odd_alternate2)
odd_str_to_int1 = list_str_to_int(odd_sep1)
odd_str_to_int2 = list_str_to_int(odd_sep2)
odd_str_2_1 = list_multiplied_by_2(odd_str_to_int1)
odd_int_to_str1 = list_int_to_str(odd_str_2_1)
odd_sep_digits1 = ""
for i in odd_int_to_str1:
odd_sep_digits1 += "".join(i)
odd_sep3 = split_char(odd_sep_digits1)
odd_str_to_int3 = list_str_to_int(odd_sep3)
odd_sum_last_add1 = sum(odd_str_to_int3)
odd_sum_last_add2 = sum(odd_str_to_int2)
odd_checksum = odd_sum_last_add1 + odd_sum_last_add2
#Printing the type of digital card based on user input
if odd_checksum % 10 == 0:
if ccn_string[:2] == "34" or "37" and len(ccn_string) == 15:
print("AMEX")
elif len(ccn_string) == 13 or 15 and ccn_string[:1] == "4":
print("VISA")
else:
print("INVALID")
else:
print("INVALID")
Ejemplo 2: Python del algoritmo de Luhn
#Better
#This is the shorter Luhn's algorithm with step-by-step instructions
#Function to split a string into a list of all its constituent characters
def split_char(number_string):
return [char for char in number_string]
#Function to convert a string list to an integer list
def list_str_to_int(test_list):
for i in range(0, len(test_list)):
test_list[i] = int(test_list[i])
return test_list
#Function to convert an integer list to a string list
def list_int_to_str(test_list):
for i in range(0, len(test_list)):
test_list[i] = str(test_list[i])
return test_list
#Function to multiply all elements of a list by 2
def list_multiplied_by_2(test_list):
for i in range(0, len(test_list)):
test_list[i] *= 2
return test_list
#Prompt the user for Input
ccn = int(input("Enter your digital card number: "))
ccn = str(ccn)
#Specifying a condition to exit the loop
if 12 > len(ccn) < 17:
print("INVALID")
exit()
num1 = " " + "".join(ccn[::-1])
num2 = "".join(ccn[::-1])
num3 = "".join(num1[::2])
num4 = "".join(num2[::2])
alternate_num3 = num3.replace(" ", "")
sep1 = split_char(alternate_num3)
sep2 = split_char(num4)
str_to_int1 = list_str_to_int(sep1)
str_to_int2 = list_str_to_int(sep2)
str_2_1 = list_multiplied_by_2(str_to_int1)
int_to_str1 = list_int_to_str(str_2_1)
sep_digits1 = ""
for i in int_to_str1:
sep_digits1 += "".join(i)
sep3 = split_char(sep_digits1)
str_to_int3 = list_str_to_int(sep3)
sum_last_add1 = sum(str_to_int3)
sum_last_add2 = sum(str_to_int2)
checksum = sum_last_add1 + sum_last_add2
#Printing the type of digital card based on user input
if checksum % 10 == 0:
if ccn[:2] == "34" or "37" and len(ccn) == 15:
print("AMEX")
elif ccn[:1] == "4" :
if len(ccn) == 13 or 14 or 15 or 16:
print("VISA")
elif ccn[:2] == "51" or "52" or "53" or "54" or "55" :
if len(ccn) == 16:
print("MASTERCARD")
else:
print("INVALID")
else:
print("INVALID")
Ejemplo 3: algoritmo luhn python
$ pip install fast-luhn
Te mostramos comentarios y valoraciones
Más adelante puedes encontrar las acotaciones de otros usuarios, tú asimismo puedes mostrar el tuyo si dominas el tema.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)