+ 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
3 Answers
+ 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
CarrieForle
CarrieForle - avatar
+ 3
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