Skip to content

Commit 84aeb06

Browse files
authored
Create luhn_check.py
1 parent 84415b2 commit 84aeb06

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

luhn_check.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def luhn_check(card_number):
2+
card_number = [int(digit) for digit in str(card_number)]
3+
checksum = 0
4+
5+
# Process each digit from right to left
6+
for i in range(len(card_number) - 1, -1, -1):
7+
digit = card_number[i]
8+
9+
# Double every second digit
10+
if (len(card_number) - i) % 2 == 0:
11+
digit *= 2
12+
# If doubling makes the digit > 9, subtract 9
13+
if digit > 9:
14+
digit -= 9
15+
16+
# Add the processed digit to checksum
17+
checksum += digit
18+
19+
# Check if the checksum is a multiple of 10
20+
return checksum % 10 == 0
21+
22+
# Test the function with an example card number
23+
print(luhn_check(4539148803436467)) # Output: False (invalid)

0 commit comments

Comments
 (0)