mirror of https://github.com/me50/kukemuna.git
61 lines
1.3 KiB
Python
61 lines
1.3 KiB
Python
|
|
import re
|
||
|
|
import sys
|
||
|
|
|
||
|
|
while True:
|
||
|
|
try:
|
||
|
|
card = input("Number: ").strip() # Remove leading and trailing whitespaces
|
||
|
|
if card.isdigit():
|
||
|
|
break
|
||
|
|
except:
|
||
|
|
ValueError
|
||
|
|
|
||
|
|
|
||
|
|
if not (len(card) == 13 or len(card) == 15 or len(card) == 16):
|
||
|
|
print("INVALID")
|
||
|
|
quit()
|
||
|
|
|
||
|
|
|
||
|
|
cardNumber = int(card)
|
||
|
|
numLength = len(card)
|
||
|
|
id = int(card[0:4].splitlines()[0])
|
||
|
|
|
||
|
|
|
||
|
|
sum = multiply = 0
|
||
|
|
|
||
|
|
# Validate cardNumber with Luhn's Algorithm
|
||
|
|
while (cardNumber > 0):
|
||
|
|
if (multiply):
|
||
|
|
if (cardNumber % 10 * 2 > 9):
|
||
|
|
n = cardNumber % 10 * 2
|
||
|
|
while (n > 0):
|
||
|
|
sum += int(n % 10)
|
||
|
|
n = int(n / 10)
|
||
|
|
else:
|
||
|
|
sum += cardNumber % 10 * 2
|
||
|
|
cardNumber = int(cardNumber / 10)
|
||
|
|
multiply = 0
|
||
|
|
else:
|
||
|
|
sum += cardNumber % 10
|
||
|
|
cardNumber = int(cardNumber / 10)
|
||
|
|
multiply = 1
|
||
|
|
|
||
|
|
if (sum % 10 != 0):
|
||
|
|
print("INVALID")
|
||
|
|
quit()
|
||
|
|
|
||
|
|
|
||
|
|
if ((int(id / 100) == 34 or int(id / 100) == 37) and numLength == 15):
|
||
|
|
print("AMEX")
|
||
|
|
quit()
|
||
|
|
|
||
|
|
if ((int(id / 1000) == 4) and (numLength == 13 or numLength == 16)):
|
||
|
|
print("VISA")
|
||
|
|
quit()
|
||
|
|
|
||
|
|
if (((int(id / 100) >= 51 and int(id / 100) <= 55) or (id >= 2221 and id <= 2720)) and numLength == 16):
|
||
|
|
print("MASTERCARD")
|
||
|
|
quit()
|
||
|
|
|
||
|
|
else:
|
||
|
|
print("INVALID")
|