ISBN校验位求解器,用户反馈

时间:2014-01-23 13:21:30

标签: python python-3.x isbn

我正在制作一个ISBN程序来解决校验位,我想这样做,当程序找到你的校验位时,会打开一个新的字符串,说“你想要关闭程序吗”,我已经这样做了。

如果他们说“n”表示“否”它返回到开头,如果该人说'y'程序关闭,我就会卡住并开始搜索互联网,我的代码在下面,任何人都可以帮助调整它,谢谢你。

HERES MY CODE:

ISBN=input("Please enter a 10 digit number for the ISBN check digit:  ")

while len(ISBN)!= 10:

    print("Please try again and make sure you entered 10 digits.")
    ISBN=int(input("Please enter the 10 digit number again: "))
    continue

else:
    D1 =int(ISBN[0])*11

    D2 =int(ISBN[1])*10
    D3 =int(ISBN[2])*9
    D4 =int(ISBN[3])*8
    D5 =int(ISBN[4])*7
    D6 =int(ISBN[5])*6
    D7 =int(ISBN[6])*5
    D8 =int(ISBN[7])*4
    D9 =int(ISBN[8])*3
    D10=int(ISBN[9])*2
    Sum=(D1+D2+D3+D4+D5+D6+D7+D8+D9+D10)
    Mod=Sum%11
    D11=11-Mod
    if D11==10:
        D11='X'
    ISBNNumber=str(ISBN)+str(D11)
    print("Your 11 digit ISBN Number is *" + ISBNNumber + "*")

def close():
    close=input ("would you like to close the program or try again 'y' for Yes and 'n' for No:")

    while len(close)==1:
        if input == "n":s
            return (ISBN)
        elif input == "y":
            exit()
close()#

2 个答案:

答案 0 :(得分:1)

最简单的方法是将所有内容包装在while循环中:

while True:
    # ... put all your code here
    close = input("Would you like to try again? Enter 'y' for Yes and 'n' for No: ")
    if close.lower() in ("n", "no"):
        print("Exiting")
        break

除非用户输入'n'(或类似内容),否则每次都会循环播放。注意:

  1. 更明确的问题,明确的是或否答案;和
  2. 使用lowerin允许可能的有效输入范围。
  3. 更广泛地说,我认为你的算法存在问题; ISBN-10编号的第10个字符(校验位)根据前九个http://en.wikipedia.org/wiki/Check_digit#ISBN_10计算。

答案 1 :(得分:1)

这会在代码中添加for循环

否则:

Sum = 0
for i in range(len(isbn)):
    sum= int(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)