-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluhn.py
39 lines (31 loc) · 857 Bytes
/
luhn.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
# Python3 program to implement
# Luhn algorithm
# Returns true if given card
# number is valid
def checkLuhn(cardNo):
nDigits = len(cardNo)
nSum = 0
isSecond = False
for i in range(nDigits - 1, -1, -1):
d = ord(cardNo[i]) - ord('0')
if (isSecond == True):
d = d * 2
# We add two digits to handle
# cases that make two digits after
# doubling
nSum += d // 10
nSum += d % 10
isSecond = not isSecond
if (nSum % 10 == 0):
return True
else:
return False
# Driver code
if __name__=="__main__":
#cardNo = "79927398713"
cardNo = "5555555555554444"
if (checkLuhn(cardNo)):
print("This is a valid card")
else:
print("This is not a valid card")
# This code is contributed by rutvik_56