Skip to content Skip to sidebar Skip to footer

Python Isbn Program

I'm trying to calculate the check digit for an ISBN input on python. so far I have... def ISBN(): numlist = [] request = raw_input('Please enter the 10 digit nu

Solution 1:

This code should get you on your way:

listofnums = [int(digit) for digit in '1234567890']
multipliers = reversed(range(2,12))
multipliednums = [a*b for a,b in zip(listofnums, multipliers)]

Strings are iterable, so if you iterate them, each element is returned as a single-character string.

int builds an int from a (valid) string.

The notation [a*b for a,b in zip(listofnums, multipliers)] is a list comprehension, a convenient syntax for mapping sequences to new lists.

As to the rest, explore them in your repl. Note that reversed returns a generator: if you want to see what is "in" it, you will need to use tuple or list to force its evaluation. This can be dangerous for infinite generators, for obvious reasons.

Solution 2:

I believe list() is what you are looking for.

numlist=list(request)

Here is how I would write the code. I hope I'm interpreting the code correctly.

defISBN():
    request = raw_input("Please enter the 10 digit number:  ")
    iflen(request) == 10:
        numlist = list(request)
        print numlist
    else:
        print"Invalid Input"

ISBN()

Solution 3:

import itertools

ifsum(x * int(d) for x, d inzip(nums, itertools.count(10, -1))) % 11 != 0:
    print"no good"

Post a Comment for "Python Isbn Program"