+ 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

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.

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

+ 2

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

Hot today

Bug: Sololearn notification is still showing number in the notification icon even i did mark as read

5 Votes

Web development

2 Votes

Finished python for beginners

1 Votes

Izzy the iguana code coach

0 Votes

C++ Question re stringstream

0 Votes

Why sometimes 0 is printed?

0 Votes