1
+ #Lab07 Credit Card Validator
2
+
3
+ def credit_card_validator (credit_card_number ):
4
+
5
+ #Convert the input string into a list of ints
6
+
7
+ cc_to_list = list (credit_card_number )
8
+
9
+ for i in range (len (cc_to_list )):
10
+ cc_to_list [i ] = int (cc_to_list [i ])
11
+
12
+ print (cc_to_list )
13
+
14
+ # Slice off the last digit. That is the check digit.
15
+
16
+ check_digit = cc_to_list .pop (- 1 )
17
+ # print(check_digit)
18
+
19
+ # Reverse the digits.
20
+
21
+ reversed_list = cc_to_list [::- 1 ]
22
+ print (reversed_list )
23
+
24
+ # Double every other element in the reversed list (starting with the first number in the list).
25
+
26
+ # for i in range(0, len(reversed_list), 2):
27
+ # reversed_list[i] = int(reversed_list[i]) * 2
28
+ # print(reversed_list[i])
29
+
30
+ double_list = []
31
+
32
+ for i in range (len (reversed_list )):
33
+ if i % 2 == 0 :
34
+ double_list .append (reversed_list [i ] * 2 )
35
+ else :
36
+ double_list .append (reversed_list [i ])
37
+
38
+ print (double_list )
39
+
40
+ # Subtract nine from numbers over nine.
41
+
42
+ subtract_list = []
43
+
44
+ for i in range (len (double_list )):
45
+
46
+ if (double_list [i ]) > 9 :
47
+ subtract_list .append (double_list [i ] - 9 )
48
+ else :
49
+ subtract_list .append (double_list [i ])
50
+
51
+ print (subtract_list )
52
+
53
+ # Sum all values.
54
+
55
+ sumall = str (sum (subtract_list ))
56
+
57
+ print (sumall )
58
+
59
+ # Take the second digit of that sum.
60
+
61
+ second_digit = sumall [1 ]
62
+
63
+ print (second_digit )
64
+
65
+ # If that matches the check digit, the whole card number is valid.
66
+
67
+ if second_digit == str (check_digit ):
68
+ print ("Valid!" )
69
+
70
+ print (credit_card_validator ("4556737586899855" ))
0 commit comments