0
Credit Card Validator
Somebody please tell me why the hell is my code not able to execute the subtraction form 9. https://code.sololearn.com/cfKQ7W1cirX5/?ref=app
7 odpowiedzi
+ 7
Ayush Ghara ,
some comments to the code:
(1) as already mentioned the digits should be converted to int values. this an be done in the same step when reversing the input.
then adapt the code according the new data format.
(2) the multiplication of the digits should not start at index 0 but from index 1 with a step of 2.
(3) the code has an issue when subtracting 9 from all numbers higher than 9.
the current code does apply this to the variable *z*, which is not correct. we have to apply it to the list *a*. (similar to the step when multiplying each second number by 2)
(4) finally we have to apply a *modulo division by 10* to the sum of the digits. the code uses *modulo 2* instead.
as already mentioned the code line 22 should be corrected.
+ 5
Orin Cook ,
the task description can be found in the *community section*, *code coach exercises* and has the name *credit card validator*
+ 4
What is your input?
edit:
According to you by
int(z) > 9 : false always..
what is the digit which is greater than 9?
+ 3
Lothar I know this, but learning to ask better questions is also an important part of everyone's education here. If op doesn't care enough about getting an answer to provide that info, I certainly don't care enough to look it up for him.
+ 3
Two more notes. Minor ones, I must say.
1. Rethink the logic of this block:
if len(a)==16:
pass
else:
print("not valid")
You only have one action (print). Why both if and else?
2. Use meaningful variable and function names. They help explain your logic to the reader.
+ 2
A couple notes, without knowing more about the problems you're running into:
* You should probably exit() if len(a)!=16, rather than just saying it's invalid and continuing anyway
* Did you mean to store the numbers in reverse order? Since it's an even number of digits, this makes an important difference for the part where you double every other number. If you wanted to store them in forward order, use a.insert(-1,x) instead.
* consider type casting a.insert(0, int(x)) to begin with to save yourself some headaches and redundant typecasting later
* else: continue is never necessary or useful lol
* line 22 i=+int(j) you probably meant +=
* consider putting your "if z >9" stuff as a subclause of the doubling operation for performance optimization, as this will reduce the number of checks the code needs to perform. It's not really necessary or meaningful for small code like this, but it's a good habit to start thinking about these issues.
+ 2
Thank you everyone for helping