C validating a credit card
Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments.Be sure to include a description of any errors you are encountering as well. As before, this routine assumes all non-digit characters have been removed from the credit card number string.The main reason I like table-driven code is because it makes the code simpler.So another task that can be useful is determining the credit card type.Listing 3: Determining a credit card's type method simply loops through this array, looking for the first description that would match the credit card number being tested.This is also explained at validate(number): # each digit in number as an int in reverse order numlist = [int(c) for c in reversed(str(number)) if c.isdigit()] count = 0 # automatic numbering for i, val in enumerate(numlist): if i % 2 == 0: count = val else: # first digit of the double value count = (2 * val) // 10 # last digit of the double value count = (2 * val) % 10 # no need for an if statement return (count % 10 == 0) def validate(number): numlist = [int(x) for x in reversed(str(number)) if x.isdigit()] # digits that count once count = sum(x for i, x in enumerate(numlist) if i % 2 == 0) # digits that count double (add digits of double value) count = sum(sum(divmod(2 * x, 10)) for i, x in enumerate(numlist) if i % 2 !
is returned if the card number doesn't match any card descriptions in the table.The Luhn Mod-10 algorithm was invented in 1954 by IBM scientist Hans Peter Luhn and is a relatively simple formula used in has numerous applications to validate identification numbers, including credit cards.The algorithm detects all single digit errors in an account number, as well as most transpositions of adjacent numbers.I've developed many websites including Black Belt Coder, Insider Articles, and others. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance.