ISBN final digit finder

感情迁移 提交于 2019-11-28 12:00:20

问题


I am working in python 3 and I am making a program that will take in a 10 digit ISBN Number and applying a method to it to find the 11th number.

Here is my current code

ISBN=input('Please enter the 10 digit number: ')

while len(ISBN)!= 10:

    print('Please make sure you have entered a number which is exactly 10 characters long.')
    ISBN=int(input('Please enter the 10 digit number: '))
    continue


else:

    Digit1=int(ISBN[0])*11
    Digit2=int(ISBN[1])*10
    Digit3=int(ISBN[2])*9
    Digit4=int(ISBN[3])*8
    Digit5=int(ISBN[4])*7
    Digit6=int(ISBN[5])*6
    Digit7=int(ISBN[6])*5
    Digit8=int(ISBN[7])*4
    Digit9=int(ISBN[8])*3
    Digit10=int(ISBN[9])*2
    Sum=(Digit1+Digit2+Digit3+Digit4+Digit5+Digit6+Digit7+Digit8+Digit9+Digit10)
    Mod=Sum%11
    Digit11=11-Mod
    if Digit11==10:
       Digit11='X'
    ISBNNumber=str(ISBN)+str(Digit11)
    print('Your 11 digit ISBN Number is ' + ISBNNumber)

I want to create some kind of loop so that the number after "Digit" for the variable name increases starting from 1 (or zero if it makes life easier), the number in the square brackets increases starting from 0 and the multiplication number to decrease from 11 to 2.

Is there any way of doing this code in a more efficient way?


回答1:


I think this should do what you want.

def get_isbn_number(isbn):
    digits = [(11 - i) * num for i, num in enumerate(map(int, list(isbn)))]
    digit_11 = 11 - (sum(digits) % 11)
    if digit_11 == 10:
        digit_11 = 'X'    
    digits.append(digit_11)
    isbn_number = "".join(map(str, digits))
    return isbn_number

EXAMPLE

>>> print(get_isbn_number('2345432681'))
22303640281810242428
>>> print(get_isbn_number('2345432680'))
2230364028181024240X

Explanation of second line:

digits = [(11 - i) * num for i, num in enumerate(map(int, list(isbn)))]

Could be written out like:

isbn_letters = list(isbn) # turn a string into a list of characters
isbn_numbers = map(int, isbn_letters) # run the function int() on each of the items in the list
digits = [] # empty list to hold the digits
for i, num in enumerate(isbn_numbers): # loop over the numbers - i is a 0 based counter you get for free when using enumerate
    digits.append((11 - i) * num) # If you notice the pattern, if you subtract the counter value (starting at 0) from 11 then you get your desired multiplier

Terms you should look up to understand the one line version of the code:
map,
enumerate,
list conprehension




回答2:


ISBN=int(input('Please enter the 10 digit number: ')) # Ensuring ISBN is an integer

while len(ISBN)!= 10:

    print('Please make sure you have entered a number which is exactly 10 characters long.')
    ISBN=int(input('Please enter the 10 digit number: '))
    continue

else:
    Sum = 0
    for i in range(len(ISBN)):
        Sum += ISBN[i]
    Mod=Sum%11
    Digit11=11-Mod
    if Digit11==10:
       Digit11='X'
    ISBNNumber=str(ISBN)+str(Digit11)
    print('Your 11 digit ISBN Number is ' + ISBNNumber)


来源:https://stackoverflow.com/questions/15558881/isbn-final-digit-finder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!