Question

I'm sure this will be a very basic question for you, but I'm making a ISBN check digit calculator for my computing class. My current code is this:

isbn = []
results = []

print("Please input your ISBN 1 number at a time")

def isbn_input():
    isbn.append(int(input("ISBN character 1: ")))
    isbn.append(int(input("ISBN character 2: ")))
    isbn.append(int(input("ISBN character 3: ")))
    isbn.append(int(input("ISBN character 4: ")))
    isbn.append(int(input("ISBN character 5: ")))
    isbn.append(int(input("ISBN character 6: ")))
    isbn.append(int(input("ISBN character 7: ")))
    isbn.append(int(input("ISBN character 8: ")))
    isbn.append(int(input("ISBN character 9: ")))
    isbn.append(int(input("ISBN character 10: ")))

isbn_input()

results.append(isbn[0] * 11)
results.append(isbn[1] * 10)
results.append(isbn[2] * 9)
results.append(isbn[3] * 8)
results.append(isbn[4] * 7)
results.append(isbn[5] * 6)
results.append(isbn[6] * 5)
results.append(isbn[7] * 4)
results.append(isbn[8] * 3)
results.append(isbn[9] * 2)

results = sum(results)
results = results % 11
results = 11 - results
print("Your ISBN is'",
      isbn[0],isbn[1],isbn[2],isbn[3],
      isbn[4],isbn[5],isbn[6],isbn[7],
      isbn[8],isbn[9],results,"'")

I know this is an insanely inefficient way of doing it, and you're probably crying even looking at it.

The first thing I did to try making it more efficient is by using a for loop. So, I changed:

def isbn_input():
    isbn.append(int(input("ISBN character 1: ")))
    isbn.append(int(input("ISBN character 2: ")))
    isbn.append(int(input("ISBN character 3: ")))
    isbn.append(int(input("ISBN character 4: ")))
    isbn.append(int(input("ISBN character 5: ")))
    isbn.append(int(input("ISBN character 6: ")))
    isbn.append(int(input("ISBN character 7: ")))
    isbn.append(int(input("ISBN character 8: ")))
    isbn.append(int(input("ISBN character 9: ")))
    isbn.append(int(input("ISBN character 10: ")))

to a for list, but I haven't figured out how to do this yet. This is the bit I need help on (Also, I know the function is pointless. Our teacher demanded we have one in, but if anyone can find a better way of doing it then that would be amazing too)

Thanks a lot for the help. Again, if you spot anything which could be made better, then please do. (Also, this is python 3.)

Was it helpful?

Solution

To simplify the input:

isbn = [int(input("ISBN character {0}: ".format(i))) 
        for i in range(1, 11)] # or 'format(i+1)' and 'range(10)'

This uses a list comprehension to simultaneously loop and built the list of integers.

Alternatively, take the whole input at once and convert it into individual integers:

isbn = list(map(int, input("Enter ISBN: ")))

Here map applies int() to convert each of the characters in the input string in turn.

OTHER TIPS

A simple for loop for input would be

for i in range(1, 11):
    isbn.append(int(input("ISBN character " + str(i) + ":")))

To read everything at once, you could do

isbninput = input("ISBN: ")

if len(isbninput) != 10:
    raise Exception("wrong number of digits")

for digit in isbninput:
   isbn.append(int(digit))

Python, being awesome, has more compact ways to write those simple loops. See jonrsharpe's answer for those. If you're still a beginner and don't fully understands how those more complex syntactic constructs work, you should do two things:

  • Prefer the code that you could write, and read, in your sleep, and the code that you find more aesthetic
  • Learn how to use them. They're awesome.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top