From 7dabaaa726c9258855e6a5704a58d77c85ed5f91 Mon Sep 17 00:00:00 2001 From: kukemuna Date: Fri, 22 Mar 2024 14:36:54 +0200 Subject: [PATCH] automated commit by check50 [check50=True] --- credit.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 credit.py diff --git a/credit.py b/credit.py new file mode 100644 index 0000000..d5e98e6 --- /dev/null +++ b/credit.py @@ -0,0 +1,60 @@ +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")