Files
  • main.py
main.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
# Uses Luhn algorithm: https://en.wikipedia.org/wiki/Luhn_algorithm
def double(n):
  digit = n*2
  if digit > 9:
    return digit-9
  else:
    return digit

error = "is"
total = 0
number = input("Please enter the card number (without spaces): ")
if len(number) > 11 and len(number) < 17:
  for char in range(2,len(number)+1):
    try:
      if char % 2:
        total += int(number[char*-1])
      else:
        total += double(int(number[char*-1]))
    except:
      error = True
  if (total*9) % 10 != int(number[-1]):
    error = "is not"
else:
  error = "is not"
print("This",error,"a valid card number.")