-
Notifications
You must be signed in to change notification settings - Fork 0
/
creditCardValidator.py
40 lines (33 loc) · 970 Bytes
/
creditCardValidator.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
# python credit card validator program
# 1. Remove any '-' or ' '
# 2. Add all digits in the odd places from right to left
# 3. Double every second digit from right to left
# (If result is a two-digit number, add the two-digit number
# together to get a single digit)
# 4. Sum the total of steps 2 & 3
# 5. If sum is divisible by 10, the credit card # is valid
sum_odd_digits = 0
sum_even_digits = 0
total = 0
# Step1
card_number = input("Enter a credit card #: ")
card_number = card_number.replace("-", "")
card_number = card_number.replace(" ", "")
card_number = card_number[::-1]
# step2
for x in card_number[::2]:
sum_odd_digits += int(x)
# step3
for x in card_number[1::2]:
x = int(x) * 2
if x >= 10:
sum_even_digits += (1 + (x % 10)) # Check the indentation of this line
else:
sum_even_digits += x
# step 4
total = sum_odd_digits + sum_even_digits
# step 5
if total % 10 == 0:
print("VALID")
else:
print("INVALID")