On Tue, Feb 19, 2013 at 1:50 PM, Morten Engvoldsen <mortene...@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)". -- http://mail.python.org/mailman/listinfo/python-list