Skip to content

Commit ee0b67f

Browse files
Merge pull request #93 from PdxCodeGuild/kacey-lab07-ccv
lab07 complete
2 parents 1aca01a + a2f084e commit ee0b67f

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

code/kaceyb/python/lab07/lab_07.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Here is a valid credit card number to test with: 4556737586899855
2+
3+
# For example, the worked out steps would be:
4+
5+
# 4 5 5 6 7 3 7 5 8 6 8 9 9 8 5 5
6+
# 4 5 5 6 7 3 7 5 8 6 8 9 9 8 5
7+
# 5 8 9 9 8 6 8 5 7 3 7 6 5 5 4
8+
# 10 8 18 9 16 6 16 5 14 3 14 6 10 5 8
9+
# 1 8 9 9 7 6 7 5 5 3 5 6 1 5 8
10+
# 85
11+
# 5
12+
# True Valid!
13+
14+
# 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:
15+
16+
17+
def credit_card_validator(credit_card_number):
18+
19+
# Convert the input string into a list of ints
20+
21+
input_list = list(credit_card_number)
22+
23+
for i in range(len(input_list)):
24+
input_list[i] = int(input_list[i])
25+
26+
print(input_list)
27+
# Slice off the last digit. That is the check digit.
28+
29+
check_digit = input_list.pop(-1)
30+
# print(check_digit)
31+
print(input_list)
32+
# Reverse the digits.
33+
input_list.reverse()
34+
print(input_list)
35+
36+
# Double every other element in the reversed list (starting with the first number in the list).
37+
38+
doubled_list = []
39+
40+
41+
for i in range(len(input_list)):
42+
if i % 2 == 0:
43+
# input_list[i] = (input_list[i]) * 2
44+
doubled_list.append(input_list[i]*2)
45+
else:
46+
# input_list[i] = input_list[i]
47+
doubled_list.append(input_list[i])
48+
49+
print(doubled_list)
50+
51+
# Subtract nine from numbers over nine.
52+
53+
subtract_nine = doubled_list
54+
55+
for i in range(len(subtract_nine)):
56+
57+
if (subtract_nine[i]) > 9:
58+
subtract_nine[i] = (subtract_nine[i]) - 9
59+
60+
print(subtract_nine)
61+
62+
# Sum all values.
63+
sum_values = str(sum(subtract_nine))
64+
print(sum_values)
65+
66+
# Take the second digit of that sum.
67+
second_digit = int(sum_values[1])
68+
print(second_digit)
69+
# If that matches the check digit, the whole card number is valid.
70+
answer = second_digit == check_digit
71+
# Return a boolean
72+
73+
return answer
74+
75+
76+
print(credit_card_validator("4556737586899855"))

0 commit comments

Comments
 (0)