Your question is Luhn Credit Card Validation. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
American Express payment flows need to reject card numbers with invalid check digits before further processing. Write a function that validates a card number using the Luhn algorithm.
Spaces and hyphens may appear as formatting characters and must be ignored. Any other non-digit character makes the input invalid. The function should return True when the resulting digit sequence passes the Luhn checksum and False otherwise.
Implement is_valid_card_number(card_number), where card_number is a string containing a card number with optional spaces or hyphens. Return a boolean. The input is valid only if it contains between 2 and 19 digits after formatting characters are removed and its Luhn checksum is valid.
To compute the checksum, scan digits from right to left. Double every second digit, beginning with the digit immediately left of the rightmost digit. If doubling produces a value greater than 9, subtract 9. The sum of all transformed digits must be divisible by 10.
card_number contains at most 50 charactersdef is_valid_card_number(card_number):