-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify_credit_card.py
60 lines (47 loc) · 1.22 KB
/
verify_credit_card.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/python3
def main():
while True:
num = input('Enter a number: ').strip()
if num != '':
try:
result = LuhnTest(num)
print(result)
except:
continue
def listDigits(num1):
digitList = []
num1 = abs(num1)
while True:
if num1 > 9:
digitList.append(num1 % 10)
num1 = num1 // 10
else:
digitList.append(num1)
return tuple(digitList[::-1])
def listDigits2(num1):
num1 = abs(num1)
if num1 < 10:
return (num1,)
else:
digit1 = num1 % 10
return listDigits2(num1 // 10) + (digit1,)
def LuhnTest(num):
backwardsNum = num[::-1]
sum1 = 0
sum2 = 0
for index, digit in enumerate(backwardsNum, 1):
if index % 2 != 0:
sum1 += int(digit)
else:
doubledDigit = int(digit) * 2
if doubledDigit > 9:
digits = listDigits(doubledDigit)
sum2 += (digits[0] + digits[1])
else:
sum2 += doubledDigit
total = sum1 + sum2
if str(total)[-1] == '0':
return True
return False
if __name__ == '__main__':
main()