query related to Luhn_Algorithm_solution:
I got the solution for validating credit card number I want to understand the logic behind second if statement where we put if condition of number*2 > 9 will be multiplied by two and subtracted by 9:
num_list = [int(char) for char in number]
num_len = len(num_list)
num_list_rev = num_list[::-1]
double = 0
single = 0
for i in range(0,num_len):
if i%2 != 0:
if 2*num_list_rev[i]>9:
double = double + 2*num_list_rev[i] - 9
else:
double = double + 2 * num_list_rev[i]
else:
single = single + num_list_rev[i] if (double+single)%10 == 0:
print(f'{number} is a valid credit card number.')
else:
print(f'{number} is not a valid credit card number.')
num_len = len(num_list)
num_list_rev = num_list[::-1]
double = 0
single = 0
for i in range(0,num_len):
if i%2 != 0:
if 2*num_list_rev[i]>9:
double = double + 2*num_list_rev[i] - 9
else:
double = double + 2 * num_list_rev[i]
else:
single = single + num_list_rev[i] if (double+single)%10 == 0:
print(f'{number} is a valid credit card number.')
else:
print(f'{number} is not a valid credit card number.')
0 answers ( 0 marked as helpful)