0
Credit Card Validator / Python
Im done guys. I can solve every Medium Code Challenge except this one. Pls dont give me new code, I want to understand the mistake in my code.. Problem: https://www.sololearn.com/coach/96?ref=app Code: input = str(input()) input1= list(input) #print(input) i_rev = input1[::-1] #print(i_rev ) new = [] for i in range(16): if i % 2 == 0 or i == 0: new.append(i_rev[i]) else: new.append(str(int(i_rev[i])*2)) #print(new) new1 = [] for i in new: if int(i) > 9: new1.append(str(int(i)-9)) else: new1.append(i) #print(new1) sum = 0 for i in new1: sum += int(i) #print(sum) if len(input) != 16: print("not valid") elif sum % 10 != 0: print('not valid') else: print('valid')
3 Antworten
+ 1
1. never use base commands (ie 'input') as a variable name
2. the challenge says to multiply each 2nd digit by 2, your first for loop has a logic error in that when i == 1, it's actually the 2nd item in the list.
(i == 2 is the 3rd and i == 3 is the 4th because we start at 0)
0
Slick thx for the answer - I'm going to avoid mistake 1.
For mistake 2 - pls activate the 'print(i_rev)' and 'print(new)' line of my code. You will see that it works and that the second digit is multiplied by 2 - I used the else statement for the multiplication wirh 2.
So I cant see your point on that one. Pls explain it to me again, im a beginner.
Thx so much!
0
Hey, I checked it and it checks out. I can't see the hidden test cases so I can't tell without it and the code is a little complicated with all the new lists and variables. Check this code out and see if you can reimagine yours. It's a little more clear cut.
All "enumerate" does is takes a list and creates another list of tuples containing 2 values, the first is the index number and the second is the actual value. So:
a = ['a', 'b', 'c']
print(enumerate(a))
# output
[(0, 'a'), (1, 'b'), (2, 'c')]
https://code.sololearn.com/c8VH9QvtNn52/?ref=app