ISBN校验数字解算器,用户feedb

2024-06-10 02:47:30 发布

您现在位置:Python中文网/ 问答频道 /正文

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

如果有人说“n”表示不,它会返回到开头,如果这个人说“y”,程序关闭,我就卡住了,开始搜索互联网,我的代码在下面,有人能帮我调整一下吗?谢谢。在

这是我的代码:

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()#

Tags: the代码程序numberforcloseinputint
2条回答

这将在代码中添加一个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)

最简单的方法是将所有内容包装在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. 使用lower和{}来允许可能的有效输入范围。在

更广泛地说,我认为您的算法有问题;ISBN-10数字的第10个字符(校验位)是根据前9个字符:http://en.wikipedia.org/wiki/Check_digit#ISBN_10计算的。在

相关问题 更多 >