1
+ # Lab 7: Credit Card Validation
2
+
3
+ # Let's write a function credit_card_validator which returns whether a string containing a credit card number is valid as a boolean. The steps are as follows:
4
+
5
+ # Convert the input string into a list of ints
6
+ # Slice off the last digit. That is the check digit.
7
+ # Reverse the digits.
8
+ # Double every other element in the reversed list (starting with the first number in the list).
9
+ # Subtract nine from numbers over nine.
10
+ # Sum all values.
11
+ # Take the second digit of that sum.
12
+ # If that matches the check digit, the whole card number is valid.
13
+
14
+ # Here is a valid credit card number to test with: 4556737586899855
15
+
16
+ # For example, the worked out steps would be:
17
+
18
+ # 4 5 5 6 7 3 7 5 8 6 8 9 9 8 5 5
19
+ # 4 5 5 6 7 3 7 5 8 6 8 9 9 8 5
20
+ # 5 8 9 9 8 6 8 5 7 3 7 6 5 5 4
21
+ # 10 8 18 9 16 6 16 5 14 3 14 6 10 5 8
22
+ # 1 8 9 9 7 6 7 5 5 3 5 6 1 5 8
23
+ # 85
24
+ # 5
25
+ # True Valid!
26
+ # ========================================================================================
27
+
28
+
29
+ enter_ccn = (input ("Enter your 16-digit credit card number: " ))
30
+ # print(len(enter_ccn))
31
+
32
+
33
+ try :
34
+ len (enter_ccn ) == 16
35
+ enter_ccn = list (enter_ccn )
36
+ print (enter_ccn )
37
+ # print("check")
38
+ except (ValueError ):
39
+ print ("Invalid number length, try again" )
40
+ print (enter_ccn )
41
+ except (ValueError ):
42
+ print ('Please enter a number' )
43
+ # ====================================================
44
+ # Convert the input string into a list of ints
45
+ # ====================================================
46
+ # print(enter_ccn)
47
+ # print(type(enter_ccn))
48
+
49
+ # ====================================================
50
+ # Slice off the last digit. That is the check digit.
51
+ # ====================================================
52
+ check_digit = enter_ccn .pop ()
53
+ print (check_digit )
54
+
55
+ # ====================================================
56
+ # Reverse the digits.
57
+ # ====================================================
58
+ enter_ccn .reverse ()
59
+ print (enter_ccn )
60
+
61
+ # =============================================================================================
62
+ # Double every other element in the reversed list (starting with the first number in the list).
63
+ # =============================================================================================
64
+
65
+ index = 0
66
+ ccn_odd_doubled = []
67
+ for odd in enter_ccn :
68
+ if int (enter_ccn [index ]) % 2 != 0 :
69
+ ccn_odd_doubled .append (int (enter_ccn [index ]) * 2 )
70
+ index += 1
71
+ else :
72
+ ccn_odd_doubled .append (int (enter_ccn [index ]))
73
+ index += 1
74
+ print (ccn_odd_doubled )
75
+
76
+
77
+ # ====================================================
78
+ # Subtract nine from numbers over nine.
79
+ # ====================================================
80
+ index = 0
81
+ for num in ccn_odd_doubled :
82
+ if ccn_odd_doubled [index ] >= 9 :
83
+ ccn_odd_doubled .pop (index )
84
+ index += 1
85
+ print (ccn_odd_doubled )
86
+ # ====================================================
87
+ # Sum all values.
88
+ # ====================================================
89
+ index = 0
90
+ sum_ccn = 0
91
+ for num in ccn_odd_doubled :
92
+ sum_ccn += ccn_odd_doubled [index ]
93
+ index += 1
94
+ print (sum_ccn )
95
+
96
+ # ====================================================
97
+ # Take the second digit of that sum.
98
+ # ====================================================
99
+ index = 1
100
+ second_digit = list (map (int , str (sum_ccn )))
101
+ # print(str(second_digit))
102
+ print (second_digit [index ])
103
+
104
+
105
+ # ====================================================
106
+ # If that matches the check digit, the whole card number is valid.
107
+ # ====================================================
108
+
109
+ if check_digit == second_digit [index ]:
110
+ print ("True Valid!" )
111
+ else :
112
+ print ("False, check digit not valid" )
0 commit comments