luhn algorithm python

#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")

0
5
SoloNasus 75 points

                                    $ pip install fast-luhn

0
0
4
10

                                    #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(&quot;Enter your digital card number: &quot;))

ccn = str(ccn)

#Specifying a condition to exit the loop
if 12 &gt; len(ccn) &lt; 17:
    print(&quot;INVALID&quot;)
    exit()

num1 = &quot; &quot; + &quot;&quot;.join(ccn[::-1])
num2 = &quot;&quot;.join(ccn[::-1])
num3 = &quot;&quot;.join(num1[::2])
num4 = &quot;&quot;.join(num2[::2])

alternate_num3 = num3.replace(&quot; &quot;, &quot;&quot;)

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 = &quot;&quot;
for i in int_to_str1:
    sep_digits1 += &quot;&quot;.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] == &quot;34&quot; or &quot;37&quot; and len(ccn) == 15:
        print(&quot;AMEX&quot;)
    elif ccn[:1] == &quot;4&quot; :
        if len(ccn) == 13 or 14 or 15 or 16:
            print(&quot;VISA&quot;)
    elif ccn[:2] == &quot;51&quot; or &quot;52&quot; or &quot;53&quot; or &quot;54&quot; or &quot;55&quot; :
        if len(ccn) == 16:
            print(&quot;MASTERCARD&quot;)
    else:
        print(&quot;INVALID&quot;)
else:
    print(&quot;INVALID&quot;)

4 (10 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source