Credit card vlidator problem | Sololearn: Learn to code for FREE!


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.

9/19/2020 12:15:14 PM

pedram ch

3 Answers

New Answer


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.


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')


CarrieForle thanks alot for your time. I did as you said and I understood my problem. Thank you so much for the help 🙏🙏