Skip to content

Commit 28449c0

Browse files
committed
credit card
1 parent 221d639 commit 28449c0

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed

Code/chadL/Python/lab6.py

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
#Lab 6: 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.
4+
The steps are as follows:
5+
6+
Convert the input string into a list of ints
7+
Slice off the last digit. That is the check digit.
8+
Reverse the digits.
9+
Double every other element in the reversed list (starting with the first number in the list).
10+
Subtract nine from numbers over nine.
11+
Sum all values.
12+
Take the second digit of that sum.
13+
If that matches the check digit, the whole card number is valid.
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+
30+
user_input = input("please enter your credit card numbers ")
31+
user_input = list(user_input)
32+
33+
def card_number(user_input):
34+
number_list = []
35+
reversed_card = []
36+
card9 = []
37+
for l in user_input:
38+
number_list.append(int(l))
39+
verify_digit1 = number_list.pop() #taking off last digit
40+
41+
for l in number_list[::-1]: #loop through reversed order
42+
reversed_card.append(l)
43+
counter = 0
44+
for l in reversed_card[0::2]: #multiply number pulled out
45+
num = (l*2)
46+
reversed_card[counter] = num
47+
counter +=2
48+
for l in reversed_card: #take 9 from value if over 9
49+
if l > 9:
50+
I9 = l - 9
51+
card9.append(I9)
52+
else:
53+
card9.append(l)
54+
card_sum = 0 #sum of all itms
55+
for l in card9:
56+
card_sum +=l
57+
card_total = str(card_sum)
58+
check_digit = card_total[-1]
59+
check_digit = int(check_digit)
60+
61+
if verify_digit1 == check_digit:
62+
return True
63+
else:
64+
return False
65+
card_number(user_input)
66+
valid = card_number(user_input)
67+
if valid == True:
68+
print("credit card is valid")
69+
elif valid == False:
70+
print("number is not valid")
71+
72+
73+
74+
75+
76+
"""
77+
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:
78+
79+
Reverse the digits.
80+
Double every other element in the reversed list (starting with the first number in the list).
81+
Subtract nine from numbers over nine.
82+
Sum all values.
83+
Take the second digit of that sum.
84+
If that matches the check digit, the whole card number is valid.
85+
Here is a valid credit card number to test with: 4556737586899855
86+
87+
For example, the worked out steps would be:
88+
89+
4 5 5 6 7 3 7 5 8 6 8 9 9 8 5 5
90+
4 5 5 6 7 3 7 5 8 6 8 9 9 8 5
91+
5 8 9 9 8 6 8 5 7 3 7 6 5 5 4
92+
10 8 18 9 16 6 16 5 14 3 14 6 10 5 8
93+
1 8 9 9 7 6 7 5 5 3 5 6 1 5 8
94+
85
95+
5
96+
True Valid!
97+
98+
------option 1-------
99+
card_no = '4556737586899855'
100+
c_card = []
101+
for char in card_no:
102+
c_card.append(int(char))
103+
------option 1-------
104+
105+
------option 2--------------
106+
c_card = list(card_no)
107+
for i in range(len(c_card)):
108+
card[i] = int(card_no[i])
109+
------option 2--------------
110+
111+
------- option 3 -----------
112+
list_cc = [int(x) for char in card_no]
113+
------- option 3 -----------
114+
115+
------option 4 -----------------
116+
list_cc = list(map(int, card_no))
117+
list_cc = list(mapped_cc)
118+
------option 4 -----------------
119+
120+
check_digit = c_card.pop()
121+
122+
c_card = c_card[::-1] or c_card.reverse() or c_card = list(reversed(c_card))
123+
-----option 1---------
124+
for index, value in enumerate(c_card):
125+
if num % 2 == 0:
126+
c_card[num] = value * 2
127+
128+
129+
130+
for index, value in enumerate(c_card):
131+
if value >9:
132+
c_card[index] = value - 9
133+
134+
or
135+
c_card = [n-9 if n> 9 else n for n in c_card]
136+
137+
138+
total = sum(c_card)
139+
140+
#take 2nd digit of that sum
141+
last_digit = total % 10
142+
143+
144+
if las_digit ==check_digit:
145+
146+
else:
147+
print("invalid credit card)
148+
149+
150+
151+
152+
153+
154+
155+
156+
157+
158+
159+
160+
161+
162+
163+
"""
164+
165+
166+
167+
168+
169+
170+
171+
172+
173+
174+

0 commit comments

Comments
 (0)