Fixed modulo 10 algorithm
This commit is contained in:
parent
f3a54fc25c
commit
57c6f1439a
15
luhn_algo.py
15
luhn_algo.py
@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# Luhn Algorithm Example - Developed by acidvegas in Python (https://git.acid.vegas/msr90)
|
# Luhn Algorithm Example - Developed by acidvegas in Python (https://git.acid.vegas/msr90)
|
||||||
|
|
||||||
def modulus10(card_number: str) -> str:
|
def modulo10(card_number: str) -> str:
|
||||||
'''
|
'''
|
||||||
Validate a card number using the Luhn Algorithm
|
Validate a card number using the Luhn Algorithm
|
||||||
|
|
||||||
@ -10,12 +10,9 @@ def modulus10(card_number: str) -> str:
|
|||||||
|
|
||||||
digits = [int(d) for d in card_number]
|
digits = [int(d) for d in card_number]
|
||||||
total_sum = 0
|
total_sum = 0
|
||||||
reversed_digits = digits[::-1]
|
|
||||||
|
|
||||||
for i in range(len(reversed_digits)):
|
for i, digit in enumerate(reversed(digits)):
|
||||||
digit = reversed_digits[i]
|
if i % 2 == 0:
|
||||||
|
|
||||||
if i % 2 != 0:
|
|
||||||
digit = digit * 2
|
digit = digit * 2
|
||||||
|
|
||||||
if digit > 9:
|
if digit > 9:
|
||||||
@ -23,7 +20,7 @@ def modulus10(card_number: str) -> str:
|
|||||||
|
|
||||||
total_sum += digit
|
total_sum += digit
|
||||||
|
|
||||||
check_digit = (10 - (total_sum % 10)) % 10
|
check_digit = (10 - total_sum % 10) % 10
|
||||||
full_card_number = card_number + str(check_digit)
|
full_card_number = card_number + str(check_digit)
|
||||||
|
|
||||||
if (total_sum + check_digit) % 10 != 0:
|
if (total_sum + check_digit) % 10 != 0:
|
||||||
@ -33,9 +30,9 @@ def modulus10(card_number: str) -> str:
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
card_number = input('Enter your card number without the last digit:')
|
card_number = input('Enter your card number without the last digit: ')
|
||||||
|
|
||||||
if not card_number.isdigit():
|
if not card_number.isdigit():
|
||||||
raise ValueError('invalid card number')
|
raise ValueError('invalid card number')
|
||||||
|
|
||||||
print(f'Full card number: {modulus10(card_number)}')
|
print(f'Full card number: {modulo10(card_number)}')
|
||||||
|
Loading…
Reference in New Issue
Block a user