Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ISBN仅接受数字(python)_Python_Numbers_Isbn - Fatal编程技术网

ISBN仅接受数字(python)

ISBN仅接受数字(python),python,numbers,isbn,Python,Numbers,Isbn,我在stackoverflow上的一些用户的帮助下创建了一个代码。我在代码中发现了一个问题。我的代码的问题是,当用户输入的ISBN错误超过两次时,它就会编码错误 这是我的密码: isbn= input('Please enter the 10 digit number: ') while not(len(isbn) == 10 and isbn.isdigit()): print('Please make sure you have entered a number which is e

我在stackoverflow上的一些用户的帮助下创建了一个代码。我在代码中发现了一个问题。我的代码的问题是,当用户输入的ISBN错误超过两次时,它就会编码错误

这是我的密码:

isbn= input('Please enter the 10 digit number: ')
while not(len(isbn) == 10 and isbn.isdigit()):
    print('Please make sure you have entered a number which is exactly 10 characters long.')
    isbn=input('Please enter the 10 digit number: '))
    continue

else:
    total= 0
   for digit in isbn: total += int(digit)
    calculation=total%11
    digit11=11-calculation
    if digit11==10:
       digit11='X'
    iSBNNumber=str(isbn)+str(digit11)
    print('Your 11 digit ISBN Number is ' + iSBNNumber)
在while循环中,以下代码尝试将输入字符串转换为int

int对象没有isdigit方法;原因归因错误

删除int。。打电话

去掉while循环中的int。事实上,只有在检查ISBN是否确实是一个数字之后,您才想将其转换为数字

此外,您不需要else或continue语句:

isbn= input('Please enter the 10 digit number: ')
while not(len(isbn) == 10 and isbn.isdigit()):
    print('Please make sure you have entered a number which is exactly 10 characters long.')
    isbn=input('Please enter the 10 digit number: ')

total= 0
for i in range(len(isbn)):
    total= int(isbn[i])
calculation=total%11
digit11=11-calculation
if digit11==10:
   digit11='X'
iSBNNumber=str(isbn)+str(digit11)
print('Your 11 digit ISBN Number is ' + iSBNNumber)

我不知道实现的算法是否正确,但上面的代码将运行。

这是完全有效的答案

isbn= input('Please enter the 10 digit number: ')
while not(len(isbn) == 10 and isbn.isdigit()):
    print('Please make sure you have entered a number which is exactly 10 characters long.')
    isbn=input('Please enter the 10 digit number: ')

total= 0
for i in range(len(isbn)):
    total= int(isbn[i])
calculation=total%11
digit11=11-calculation
if digit11==10:
   digit11='X'
iSBNNumber=str(isbn)+str(digit11)
print('Your 11 digit ISBN Number is ' + iSBNNumber)

你有最好的答案。我可以让你投票支持我吗?这样我就可以投票支持你。投票支持有趣的问题和答案,此外,你需要15个代表点才能投票支持。换个角度考虑。这是说嘿,这个答案正是我所需要的。我认为ISBN数字可以从0开始,因此它们不应该被转换为数字。你不能问关于课程的问题,然后隐藏问题。如果你不想让你的教授看到,那么一开始就不要把它贴在这里。把你的答案贴在你的问题上也没用。在发布下一个问题之前,您需要进一步了解该网站的工作原理。另见
>>> len(isbn)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'int' has no len()
isbn = input('Please enter the 10 digit number: ')
isbn= input('Please enter the 10 digit number: ')
while not(len(isbn) == 10 and isbn.isdigit()):
    print('Please make sure you have entered a number which is exactly 10 characters long.')
    isbn=input('Please enter the 10 digit number: ')

total= 0
for i in range(len(isbn)):
    total= int(isbn[i])
calculation=total%11
digit11=11-calculation
if digit11==10:
   digit11='X'
iSBNNumber=str(isbn)+str(digit11)
print('Your 11 digit ISBN Number is ' + iSBNNumber)
isbn= input('Please enter the 10 digit number: ')
while not(len(isbn) == 10 and isbn.isdigit()):
    print('Please make sure you have entered a number which is exactly 10 characters long.')
    isbn=input('Please enter the 10 digit number: ')

total= 0
for i in range(len(isbn)):
    total= int(isbn[i])
calculation=total%11
digit11=11-calculation
if digit11==10:
   digit11='X'
iSBNNumber=str(isbn)+str(digit11)
print('Your 11 digit ISBN Number is ' + iSBNNumber)