-
Notifications
You must be signed in to change notification settings - Fork 1
/
credit.py
59 lines (47 loc) · 1.3 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
58
59
def determineCompany(number):
'''determine the company accroding the number'''
first = number[0:2]
if first == "34" or first == "37":
return "AMEX"
elif first == "51" or first == "52" or first == "53" or first == "54" or first == "55":
return "MASTERCARD"
elif number[0] == "4":
return "VISA"
else:
return "INVALID"
def validateNumber(number):
'''validate the credit card number'''
switcher = False
other = 0
even = 0
for c in reversed(number):
if switcher:
i = int(c) * 2
# if its greater split and add up
if i > 9:
other += int(str(i)[0])
other += int(str(i)[1])
else:
other += i
else:
even += int(c)
switcher = not switcher
# return modulo to check if last is 0
return (other + even) % 10
def main():
print("Number: ")
number = input()
# number great enough?
if int(number) > pow(10, 12):
# get the company
res = determineCompany(number)
if res == "INVALID":
print(res)
return
# number correct?
if validateNumber(number):
res = "INVALID"
print(res)
# checking input function
if __name__ == "__main__":
main()