-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcredit_card_validation.py
More file actions
51 lines (38 loc) · 1.28 KB
/
credit_card_validation.py
File metadata and controls
51 lines (38 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#Let's write a function which returns whether a string containing a credit card number is valid as a boolean. The steps are as follows:
# user input credit card digits
user_input = input("Enter your credit cared number: ")
# print(type(user_input))
#Convert the input string into a list of ints
user_input = list(map(int, user_input))
print(user_input)
# Slice off the last digit. That is the check digit.
popped_digit = user_input.pop()
print(user_input)
# Reverse the digits.
def digit_reversed(user_input):
new_list = user_input[::-1]
return new_list
new_result = digit_reversed(user_input)
print(new_result)
# Double every other element in the reversed list.
for i in range(len(new_result)):
if i % 2 == 0:
new_result[i] = new_result[i]*2
# print(new_result)
print(new_result)
# Subtract nine from numbers over nine.
for x in range(len(new_result)):
if new_result[x] > 9:
new_result[x] = new_result[x] - 9
# print(sum(new_result)) # Sum all values.
print(new_result)
print(sum(new_result))
# Take the second digit of that sum.
net = (sum(new_result)) % 10
print(net)
# If that matches the check digit, the whole card number is valid.
final_check = net - popped_digit
if final_check == 0:
print("Valid!")
else:
print("Invalid")