-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathTask-creditCard.py
56 lines (52 loc) · 1.74 KB
/
Task-creditCard.py
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
52
53
54
55
56
#TO DO: PASS a dictionary of card numbers to this function to calculate and validate
def validate_creditCard_check_digit():
# get the card number as an input
cardNumber=input('add your card number to validate: ')
# pass the number to a list to split the digits
temp = [int(x) for x in str(cardNumber)]
print(temp)
number_len=len(temp)
print(number_len)
# from right get every second digit
result=0
total=0
checkDigit= temp[-1]
validate_on_list=sorted(temp[-2::-2] , reverse= True)
print(validate_on_list)
for item in validate_on_list:
result=item*2
print(result)
if result>9:
result-=9
else:
total+=result
print(total)
validator=(checkDigit+total)%10
print(validator)
if validator==0:
print ('Valid CardNamber')
else:
print ('invalid card Namber')
# if digit*2 = result > 9 => x = x[0]+x[1] or result-=9 , these are products
# sum up all products together = total
# (checkDigit+ total)%10==0
# if last digit is == checkDigit
#then return true
def calculate_gcreditCard_check_digit():
#get the card number as in input
cardNumber= input('add your card number to calculate the check digit: ')
#create a list and pass the card number to the list , splits each digit to a cell in the list
temp=[int(x) for x in cardNumber]
#keep the last digit as actual check digit or remove it
number_len=len(temp)
calculate_on_list=temp[:number_len]
# clculate the check digit
total=0
# sum up all the digits in the list
for item in calculate_on_list:
total+=item
# (sum *9)%10= check digit
print((total*9)%10)
#return the check digit
validate_creditCard_check_digit()
calculate_gcreditCard_check_digit()