The number we're going to use is 5893804115457289, which is a valid Maestro card number, but not one which is in use. Let's look at this step by step for a valid number so we can see this in action. If it's not, the card number is not valid. If the result is divisible by 10, the number is a valid card number.Add together all of the results and add the checking digit.If any of the results are greater than 9, subtract 9 from those numbers. For this sequence of reversed digits, take the digits at each of the even indices ( 0, 2, 4, 6, etc.) and double them.Reverse the order of the remaining digits.This number is called the checking digit, and it will be excluded from most of our calculations. Remove the rightmost digit from the card number.The way we're going to use the algorithm is as follows: The purpose of the algorithm is to identify potentially mistyped numbers, because it can determine whether or not it's possible for a given number to be the number for a valid card. This algorithm is actually used in real-life applications to test credit or debit card numbers as well as SIM card serial numbers. The algorithm we're going to use to verify card numbers is called the Luhn algorithm, or Luhn formula. Remember, you can always look back at your notes, or back the previous days' posts if you forget how something works! A quick explanation of the algorithm After that, we'll present a model solution as part of a code along walkthrough.Īs always, try your best to do this on your own, but if you get really stuck, follow along with the solution until you feel like you can finish it yourself. You'll find this explanation below, along with the project brief. It's actually a lot easier than you might think, and I'm going to explain the algorithm we're going to use in detail to help you out. When the program is complete, we're going to be able to determine whether a given card number is valid or not. I would use re.VERBOSE to improve the formatting of the regex.Welcome to the day 9 project for the 30 Days of Python series! For this project, we're going to be writing a simple credit card validator. I would change check_sum to checksum to make it clear that it returns a value and not a boolean. sum_digits should in turn be def sum_digits(number): Return sum(odds) + sum(EVEN_VALUES for i in evens)įWIW, I think sum(sum_digits(2 * i) for i in evens) I would also convert to integers at that point, not in the return: numeric_string = ] I find this very confusing: odds = numeric_stringīetter is to reverse the string numeric_string = numeric_stringĪnd do obvious slices: odds = numeric_string You don't need to write setUp in the tests if it does nothing, so remove that. These are on top of 200_success' suggestions because I agree with them and there's no point me repeating them. Return check_regex(candidate) and check_luhn(candidate) Return check_regex(candidate) and valid_length_cc(candidate) and is_luhn_valid(candidate) Return (check_sum(candidate) + check_digit) % 10 = 0 Return (check_sum(numeric_string) * 9) % 10 Return len(cc_candidate) = 16 or len(cc_candidate) = 15 Self.assertFalse(luhn_check.is_valid_cc("11abcdefg")) Self.assertFalse(luhn_check.is_valid_cc("abcdefg")) Self.assertFalse(luhn_check.is_luhn_valid("abcdefg")) Here are the tests: from unittest import TestCase I'm using luhn algorithm and regex to check credit card numbers for validity. Feedback about both the tests and the code is appreciated. I am practicing test-driven development in Python.
0 Comments
Leave a Reply. |