Split an integer into digits to compute an ISBN checksum
Just create a string out of it.
myinteger = 212345number_string = str(myinteger)
That's enough. Now you can iterate over it:
for ch in number_string: print ch # will print each digit in order
Or you can slice it:
print number_string[:2] # first two digitsprint number_string[-3:] # last three digitsprint number_string[3] # forth digit
Or better, don't convert the user's input to an integer (the user types a string)
isbn = raw_input()for pos, ch in enumerate(reversed(isbn)): print "%d * %d is %d" % pos + 2, int(ch), int(ch) * (pos + 2)
For more information read a tutorial.
while number: digit = number % 10 # do whatever with digit # remove last digit from number (as integer) number //= 10
On each iteration of the loop, it removes the last digit from number, assigning it to digit
. It's in reverse, starts from the last digit, finishes with the first
list_of_ints = [int(i) for i in str(ISBN)]
Will give you a ordered list of ints. Of course, given duck typing, you might as well work with str(ISBN).
Edit: As mentioned in the comments, this list isn't sorted in the sense of being ascending or descending, but it does have a defined order (sets, dictionaries, etc in python in theory don't, although in practice the order tends to be fairly reliable). If you want to sort it:
list_of_ints.sort()
is your friend. Note that sort() sorts in place (as in, actually changes the order of the existing list) and doesn't return a new list.