commit f7f6cadddc34d5e88d5169a36cf4565d17c918b5 Author: bot50 Date: Fri Mar 22 12:36:58 2024 +0000 kukemuna-cs50/problems/2024/x/sentimental/credit@20240322T123658.828443263Z 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")