Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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
Python 如何要求输入验证为10位整数?_Python_Python 3.x_Integer_Isbn - Fatal编程技术网

Python 如何要求输入验证为10位整数?

Python 如何要求输入验证为10位整数?,python,python-3.x,integer,isbn,Python,Python 3.x,Integer,Isbn,我正在制作一个ISBN校验数字程序。然而,尽管我的程序只接受长度为10的值,但如果我输入10个字母,它将崩溃 有人知道如何解决这个问题吗 我的代码: isbnnumber = input('Please input your 10 digit book no. : ') while len(isbnnumber) != 10: print('You have not entered a 10 digit value! Please try again.') isbnnumber

我正在制作一个ISBN校验数字程序。然而,尽管我的程序只接受长度为10的值,但如果我输入10个字母,它将崩溃

有人知道如何解决这个问题吗

我的代码:

isbnnumber = input('Please input your 10 digit book no. : ')
while len(isbnnumber) != 10:
    print('You have not entered a 10 digit value! Please try again.')
    isbnnumber = input('Please input your 10 digit book no. : ')
else:
    no1 = int(isbnnumber[0])*11
    no2 = int(isbnnumber[1])*10... etc...
非常感谢您的帮助。

您可以使用测试字符串是否都是数字:

while len(isbnnumber) != 10 or not isbnnumber.isdigit():
请参见下面的演示:

>>> '123'.isdigit()
True
>>> '123a'.isdigit()
False
>>>
>>>
>>> isbnnumber = input('Please input your 10 digit book no. : ')
Please input your 10 digit book no. : 123
>>> while len(isbnnumber) != 10 or not isbnnumber.isdigit():
...     print('You have not entered a 10 digit value! Please try again.')
...     isbnnumber = input('Please input your 10 digit book no. : ')
...
You have not entered a 10 digit value! Please try again.
Please input your 10 digit book no. : 123456789
You have not entered a 10 digit value! Please try again.
Please input your 10 digit book no. : 123456789a
You have not entered a 10 digit value! Please try again.
Please input your 10 digit book no. : 1234567890
>>>
您可以使用测试字符串是否都是数字:

while len(isbnnumber) != 10 or not isbnnumber.isdigit():
请参见下面的演示:

>>> '123'.isdigit()
True
>>> '123a'.isdigit()
False
>>>
>>>
>>> isbnnumber = input('Please input your 10 digit book no. : ')
Please input your 10 digit book no. : 123
>>> while len(isbnnumber) != 10 or not isbnnumber.isdigit():
...     print('You have not entered a 10 digit value! Please try again.')
...     isbnnumber = input('Please input your 10 digit book no. : ')
...
You have not entered a 10 digit value! Please try again.
Please input your 10 digit book no. : 123456789
You have not entered a 10 digit value! Please try again.
Please input your 10 digit book no. : 123456789a
You have not entered a 10 digit value! Please try again.
Please input your 10 digit book no. : 1234567890
>>>

您可以将while循环的条件替换为检查数字的更丰富的条件,例如:

while not isbnnumber.isdigit() or len(isbnnumber) != 10:    

您可以将while循环的条件替换为检查数字的更丰富的条件,例如:

while not isbnnumber.isdigit() or len(isbnnumber) != 10:    

请注意,不仅有ISBN-10,还有ISBN-13(事实上,在世界范围内更常用)。 此外,ISBN-10不一定是所有数字:一个数字是校验和,它可以计算为字母“X”(当它改为10时,用数字表示)。 当你这样做的时候:也检查那些校验和数字;他们在那里是有原因的

因此,我建议您制作一些助手函数:

def is_valid_isbn(isbn):
    return is_valid_isbn10(isbn) or is_valid_isbn13(isbn)

def is_valid_isbn10(isbn):
    # left as an exercise
    return len(...) and isbn10_validate_checksum(isbn)

def is_valid_isbn13(isbn):
    # left as an exercise
    return len(...) and isbn13_validate_checksum(isbn)
并按如下方式实现输入循环:

valid_isbn=False
while not valid_isbn:
    isbn = input('Please input your ISBN: ')
    valid_isbn = is_valid_isbn(isbn) and isbn # and part is optional, little trick to save your isbn directly into the valid_isbn variable when valid, for later use.

请注意,不仅有ISBN-10,还有ISBN-13(事实上,在世界范围内更常用)。 此外,ISBN-10不一定是所有数字:一个数字是校验和,它可以计算为字母“X”(当它改为10时,用数字表示)。 当你这样做的时候:也检查那些校验和数字;他们在那里是有原因的

因此,我建议您制作一些助手函数:

def is_valid_isbn(isbn):
    return is_valid_isbn10(isbn) or is_valid_isbn13(isbn)

def is_valid_isbn10(isbn):
    # left as an exercise
    return len(...) and isbn10_validate_checksum(isbn)

def is_valid_isbn13(isbn):
    # left as an exercise
    return len(...) and isbn13_validate_checksum(isbn)
并按如下方式实现输入循环:

valid_isbn=False
while not valid_isbn:
    isbn = input('Please input your ISBN: ')
    valid_isbn = is_valid_isbn(isbn) and isbn # and part is optional, little trick to save your isbn directly into the valid_isbn variable when valid, for later use.
使用,您可以精确地测试给定字符串的定义格式

import re

m = re.match(r'^\d{10}$', isbn)
if m:
    # we have 10 digits!
这里的正则表达式是
\d{10}
\d
表示您正在查找一个数字,
{10}
表示您需要正好十个数字。
^
标记字符串的开头,
$
标记字符串的结尾

使用正则表达式并不总是您需要的,如果您第一次使用正则表达式,您需要一些时间来理解。但是正则表达式是最强大的开发工具之一。

使用,您可以精确地测试给定字符串的定义格式

import re

m = re.match(r'^\d{10}$', isbn)
if m:
    # we have 10 digits!
这里的正则表达式是
\d{10}
\d
表示您正在查找一个数字,
{10}
表示您需要正好十个数字。
^
标记字符串的开头,
$
标记字符串的结尾


使用正则表达式并不总是您需要的,如果您第一次使用正则表达式,您需要一些时间来理解。但是正则表达式是最强大的开发工具之一。

您使用的是Python 3.x吗?如果是这样,您应该用“Python-3.x”标记它是的,我会的。有什么帮助吗?许多ISBN包括字母X和数字,任何字符到数字的限制都会在这些ISBN上被打破。当用作校验位时,大写X表示数字10。您使用的是Python 3.X吗?如果是这样,您应该用“Python-3.x”标记它是的,我会的。有什么帮助吗?许多ISBN包括字母X和数字,任何字符到数字的限制都会在这些ISBN上被打破。大写X表示数字10,当用作校验位时,我怎么说不是数字?这是非常有用的,但我需要相反的方式来适应我的程序。@user3576688它确实这样说。它基本上是这样写的:
循环这个字符串,直到字符串长度为10个字符并且由所有数字组成
大写X可以用作ISBNs中的校验位,OP没有提到我如何说它不是数字?这是非常有用的,但我需要相反的方式来适应我的程序。@user3576688它确实这样说。它基本上是这样写的:
循环这个字符串,直到字符串长度为10个字符,并且由所有数字组成
大写X可以用作ISBNs中的校验位,OP没有提到当用作校验位时处理大写X的向上投票,当用作校验位时处理大写X的向上投票