Luhn Checksum for Card Numbers

easy
Unit 6 · Decimal & Validation0/3 to milestone

The Luhn algorithm catches typos in card and account numbers. From the rightmost digit, double every second digit (subtracting 9 if the result is over 9), sum everything, and the number is valid if the total is divisible by 10.

Implement:

  • luhn_valid(number): accepts a string that may contain spaces or hyphens; any other non-digit character, or fewer than 2 digits, makes it invalid
  • luhn_check_digit(partial): the digit to append to a digit string to make it valid
#validation#algorithms2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Luhn Checksum for Card Numbers · TechGradify