From 377b713b836ee8f640c3f4737e9375ffaa7319bd Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Sat, 7 Jan 2023 16:58:33 +0200 Subject: [PATCH] Add Lesson 3 Task K3F9#@%^&? --- Lesson_3/We know/bank_card.py | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Lesson_3/We know/bank_card.py diff --git a/Lesson_3/We know/bank_card.py b/Lesson_3/We know/bank_card.py new file mode 100644 index 0000000..a8cd975 --- /dev/null +++ b/Lesson_3/We know/bank_card.py @@ -0,0 +1,37 @@ +def validateLength(number): + if len(number) < 13 or len(number) > 16 or len(number) == 14: + return False + return True + +def validateLuhn(number): + even = False + evenDigits = "" + evenSum = 0 + oddSum = 0 + for i in reversed(range(len(number))): + if even: + evenDigits += str(int(number[i]) * 2) + else: + oddSum += int(number[i]) + even = not even + + for i in range(len(evenDigits)): + evenSum += int(evenDigits[i]) + + if (evenSum + oddSum) % 10 == 0: + return True + return False + +number = input("Number: ") +if validateLength(number) and validateLuhn(number): + if len(number) == 13 or len(number) == 16 and number[0] == "4": + print("VISA") + elif len(number) == 16 and number[0] == "5" and int(number[1]) >= 1 and int(number[1]) <= 5: + print("MASTERCARD") + elif len(number) == 15 and number[0] == "3" and number[1] == "4" or number[1] == "7": + print("AMEX") + else: + print("INVALID") +else: + print("INVALID") +