https://github.com/calcuis/credit-card-validator-py
https://github.com/calcuis/credit-card-validator-py
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/calcuis/credit-card-validator-py
- Owner: calcuis
- Created: 2023-09-20T23:32:06.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-09-20T23:44:03.000Z (over 1 year ago)
- Last Synced: 2025-01-21T13:11:50.353Z (5 months ago)
- Language: Python
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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