The Credit Card Validator

Computer Science | Artificial Intelligence | Computational Biology | Chess | Blender

Credit Card Validator

This is a simple python script that validates credit card numbers using the Luhn algorithm. The Luhn algorithm is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers in the United States, and Canadian Social Insurance Numbers.

Below is the project.


Credit Card Validator

# Credit Card Validator Program
card_number = input("Enter a card number ")# Enter a card number
card_number = card_number.replace(" ","")# Remove spaces in number
card_number = card_number.replace("-","")# Remove underscores in number
card_number = card_number[::-1]# Reverse the number

# Create variables
sum_of_odd_nums = 0
sum_of_even_nums = 0
total = 0

# Loop through and sum all odd placed numbers
for i in card_number[::2]:
    sum_of_odd_nums += int(i)

# Lopp through and double all even placed numbers
for i in card_number[1::2]:
    x = int(i) * 2
    # If x is greater than 9, split it and add them all up
    if x >= 10:
        sum_of_even_nums += (1 + (int(x) % 10))
    # Else it is below 10, sum all up
    else:
        sum_of_even_nums += int(x)

# Total all summed even and summed odd variables
total = sum_of_even_nums + sum_of_odd_nums

# If total is divisible by 10, then valid
if total % 10 == 0:
    print("Valid")
# Else, it is invalid
else:
    print("Invalid")