Verification of bank number using modulus 11
Ian Kelly
ian.g.kelly at gmail.com
Tue Feb 19 22:10:42 CET 2013
On Tue, Feb 19, 2013 at 1:50 PM, Morten Engvoldsen <mortenengv at gmail.com> wrote:
> Here is my code:
> def calc_checkdigit(isbn):
> isbn = isbn.replace(".", "")
> check_digit = int(isbn[-1])
> isbn = isbn[:-1]
> if len(isbn) != 10:
> return False
> result = sum((10 - i) * (int(x) if x != 'X' else 10) for i, x in
> enumerate(isbn))
> return (result % 11) == check_digit
>
> calc_checkdigit(""8601.11.17947"")
>
> In my program : it is calculating 10 digit with weights 10-1, but according
> to above algorithm the weights should be : 5, 4, 3, 2, 7, 6, 5, 4, 3, 2.
>
> Could you please let me know how can i calculate the 10 digit with weights
> 5, 4, 3, 2, 7, 6, 5, 4, 3, 2. I am using python 2.7.
Use zip with the weight sequence instead of enumerate:
weights = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2]
result = sum(w * (int(x) if x != 'X' else 10) for w, x in
zip(weights, isbn))
Do the account numbers actually use 'X', or is that just left over
from the ISBN algorithm? If not, then you could replace "(int(x) if x
!= 'X' else 10)" with just "int(x)".
More information about the Python-list
mailing list