+ 1

Credit card vlidator problem

I wrote this code for cc validator challange but It doesn't work for all cases. Can anyone help me with this please? Chalange tasks : ● Number should be exactly 16 digits ● You need to verify if the given credit card number is valid. For that you need to use the Luhn test. Here is the Luhn formula: 1. Reverse the number. 2. Multiple every second digit by 2. 3. Subtract 9 from all numbers higher than 9. 4. Add all the digits together. 5. Modulo 10 of that sum should be equal to 0. https://code.sololearn.com/cc8uK71oBtDv/?ref=app

19th Sep 2020, 12:15 PM
pedram ch
pedram ch - avatar
4 odpowiedzi
+ 1
Use for n in range(16) instead of num, with that, change all the n to num[n]. Change if num.index(n) % 2 == 0 to n % 2 == 1 because what it checks is every second digit.
19th Sep 2020, 1:21 PM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 5
num = list(input()[::-1]) for i in range(1, len(num)+1, 2): num[i] = str((int(num[i]))*2) num_2 = num for i in range(0, len(num_2)): if int(num_2[i]) > 9: num_2[i] = str(int(num_2[i])-9) print('valid' if len(num_2) == 16 and sum([int(i) for i in num_2 ]) % 2 == 0 else 'not valid')
24th Nov 2021, 1:23 PM
Mas'ud Saleh
Mas'ud Saleh - avatar
+ 2
CarrieForle thanks alot for your time. I did as you said and I understood my problem. Thank you so much for the help 🙏🙏
19th Sep 2020, 1:32 PM
pedram ch
pedram ch - avatar
0
My solution: xcreditcardnum = input() new_x_c_n = list(xcreditcardnum).reverse().join() newstp1 = new_x_c_n for k in new_x_c_n: if (newstp1.index(k,0,15) + 1)%2 == 0: newstp1[newstp1.index(k,0,15)]*=2 for i in newstp1: if i > 9: newstp1[newstp1.index(i,0,15)] -= 9 suM = sum(newstp1[:]) chck = suM%10 if chck == 0: print('valid') else: print('invalid') The only problem is I need to join. I can't can anybody help?
29th May 2023, 10:27 AM
IsantosDowd
IsantosDowd - avatar