11#Credit Card Validator
2+ #4556737586899855
3+ # card_number = input("What is the card number?: ")
4+
5+ # c_card =[]
6+ # for char in c_card:
7+ # c_card.append(int(char))
8+
9+ #Another Option (python is reading this from right to left..for every number in this list..convert to int)
10+ #nums = [int(char) for char in card_number]
11+ card_number = '4556737586899855'
12+
13+
14+
215
3- card_number = input ("What is the card number?: " )
416#Convert input strings to list
5- nums = list (card_number )
17+ def credit_card_validator ():
18+ nums = list (card_number )
19+
20+ for i in range (len (nums )):
21+ nums [i ] = int (nums [i ])
22+ print (int (nums [i ]))
23+
24+ print (nums )
25+
26+ #Slice off the last digit (now called check digit)
27+ check_digit = nums .pop ()
28+
29+ #Reverse List
30+ nums = nums [::- 1 ]
31+ print (nums )
32+
33+ #Another method to reverse the list
34+ # check_digit.reverse()
35+ # nums = list(reversed(nums)
36+
37+ #Double every other element in the reversed list
38+ for x in range (0 , len (nums ), 2 ):
39+ nums [x ] += nums [x ]
40+
41+ print (nums )
42+
43+ #Option 1
44+ # for index, value in enumerate(nums):
45+ # if index % 2 == 0:
46+ # nums[index] = value *2
47+
48+ # Subtract nine from numbers over nine.
49+ #Option 1
50+ for index , value in enumerate (nums ):
51+ if value > 9 :
52+ nums [index ] = value - 9
53+
54+ print (nums )
655
7- for i in range (len (nums )):
8- nums [i ] = int (nums [i ])
9- print (int (nums [i ]))
56+ # Sum all values.
57+ total = sum (nums )
1058
11- #Slice off the last digit (now called check digit)
12- check_digit = nums [0 :15 ]
59+ print (total )
1360
14- print (check_digit )
1561
16- #Reverse List
17- check_digit .reverse ()
62+ # Take the second digit of that sum.
1863
19- print ( check_digit )
64+ last_digit = total % 10
2065
66+ print (last_digit )
2167
68+ # If that matches the check digit, the whole card number is valid.
69+ if last_digit == check_digit :
70+ return True
71+ else :
72+ return False
73+
74+ print (credit_card_validator ())
0 commit comments