-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredit.py
57 lines (47 loc) · 1.44 KB
/
credit.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Prototype
def main():
card_number = get_card()
checksum = calc_checksun(card_number)
check_card_type(card_number, checksum)
# Prompting the user for number
def get_card():
while True:
card_number = input("Number: ")
if card_number.isnumeric():
break
# retuning the card number
return card_number
# odd number
def calc_checksun(card_number):
even_sum = 0
odd_sum = 0
card_number = reversed([int(digit) for digit in card_number])
for i, digit in enumerate(card_number):
if (i + 1) % 2 == 0:
odd_digit = digit * 2
if odd_digit > 9:
odd_sum += int(odd_digit / 10) + odd_digit % 10
else:
odd_sum += odd_digit
else:
even_sum += digit
checksum = even_sum + odd_sum
return checksum
# defining & checking card type
def check_card_type(card_number, checksum):
start_number = int(card_number[0:2])
card_lenght = len(card_number)
checksum_last_digit = checksum % 10
if checksum_last_digit == 0:
if start_number in [34, 37] and card_lenght == 15:
print("AMEX")
elif (int(card_number[0]) == 4) and card_lenght in [13, 16]:
print("VISA")
elif (start_number in range(51, 56)) and card_lenght == 16:
print("MASTERCARD")
else:
print("INVALID")
else:
print("INVALID")
# Returning main function
main()