An open API service indexing awesome lists of open source software.

https://github.com/calcuis/credit-card-validator-py


https://github.com/calcuis/credit-card-validator-py

Last synced: 3 months ago
JSON representation

Awesome Lists containing this project

README

        

## credit card validator (Luhn algorithm) in Python

This Python code defines a credit card number validator using the Luhn algorithm, which is a simple checksum formula used to validate various identification numbers, including credit card numbers.

Let's break down the code step by step:

`get_digit`(number):
- This function takes a number as an argument and returns the sum of the digits of that number. It does so by using the modulo operator (%) to get the last digit and the integer division (//) to get the second-to-last digit. It then adds these two digits and returns the result.

`sum_odd_digits`(card_number)`:
- This function calculates the sum of the digits at odd indices (1-indexed) of the card number. It iterates through the card number in reverse order, starting from the last digit, and sums the digits at odd indices.

`sum_even_digits`(card_number):
- This function calculates the sum of the doubled digits at even indices (1-indexed) of the card number. It iterates through the card number in reverse order, starting from the second-to-last digit, and doubles the digit, then calls the `get_digit()` function to sum the digits of the doubled number. Finally, it sums these digits.

Main Section `(if name == "main")`:
- The program prompts the user to enter a credit card number.
- It calculates the sum of the even-indexed digits by calling `sum_even_digits()` and the sum of the odd-indexed digits by `calling sum_odd_digits()`.
- It adds these two sums and checks if the total sum is divisible by 10 (i.e., if it's a multiple of 10). If it is, the card number is considered valid according to the Luhn algorithm.
- Finally, it prints whether the entered credit card number is valid or not based on the Luhn algorithm.

Overall, this script implements a basic credit card number validator using the Luhn algorithm to check the validity of the entered credit card number.

## Reference
https://github.com/calcuis/credit-card-validator