+ 2
Python Problem: CREDIT CARD VALIDATOR
While trying to solve the codecoach problem mentioned above, I wrote following code which passed only 5 out of 7 tests. Now I am stuck and can't understand the mistakes I did. Request you all to ponder over it a bit and help me fix bug in my code. Thanks. My code -- Problem link -- https://www.sololearn.com/coach/96?ref=app cc_number = input() cc_number = list(map(int,cc_number)) cc_number.reverse() for i in range(1,16,2): cc_number[i]*=2 for i in range(16): if cc_number[i]>9: cc_number[i] -=9 sum_cc = sum(cc_number) if sum_cc % 10 == 0 and len(cc_number)==16: print("valid") else: print ("not valid")
7 ответов
+ 3
CHANDAN ROY
If input length is not equal to 16 then your loop will not work.
You first need to check if input length is equal to 16 then start loop.
I sent you in DM. Check what was mistake.
+ 1
I Am Groot !
Sir, please help.
+ 1
def test_luhna (code):
op_1 = list(map(int, (code[::-1])))
op_2 = [i*2 for i in op_1[1::2]]
op_1[1::2] = op_2
for n, i in enumerate(op_1, 0):
if i > 9:
i -= 9
op_1[n] = i
if sum(op_1) % 10 == 0 and len(op_1) == 16:
print('valid')
else:
print('not valid')
return
card_number = input()
test_luhna(card_number)
+ 1
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)
if len(num_2) == 16 and sum([int(i) for i in num_2 ]) % 2 == 0:
print('valid')
else:
print('not valid')
0
спасибо
0
n = input()[::-1]
s= 0
for i in range(len(n)):
if i%2 ==0 or i ==0 :
s += (int(n[i]))
else:
if int(n[i])*2 > 9:
s += (int(n[i])*2-9)
else :
s += (int(n[i])*2)
print("valid" if s%10 ==0 and len(n) == 16 else "not valid")
0
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')