【问题标题】:How do you require an input to validate as an integer with 10 digits?您如何要求输入验证为 10 位整数?
【发布时间】:2014-04-29 21:38:31
【问题描述】:

我正在制作一个 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...

非常感谢您的帮助,谢谢。

【问题讨论】:

  • 您使用的是 Python 3.x 吗?如果是这样,你应该用'Python-3.x'标记它
  • 是的,我会这样做。对这个问题有帮助吗?
  • 许多 ISBN 包含字母 X 和数字,任何对数字的字符限制都会在这些 ISBN 上中断。大写 X 表示用作校验位时的数字 10 isbn.org/faqs_general_questions#isbn_faq5

标签: python python-3.x integer isbn


【解决方案1】:

您可以使用str.isdigit 来测试一个字符串是否全为数字:

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
>>>

【讨论】:

  • 怎么说不是数字?这很有帮助,但我需要反过来才能适应我的程序。
  • @user3576688 它确实这么说。它基本上说:loop this until the string is 10 characters long and consists of all digits
  • 大写 X 可用作 ISBN 中的校验位,OP 未提及 isbn.org/faqs_general_questions#isbn_faq5
【解决方案2】:

请注意,不仅有 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.

【讨论】:

【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    使用regular expressions,您可以准确地测试给定字符串的定义格式。

    import re
    
    m = re.match(r'^\d{10}$', isbn)
    if m:
        # we have 10 digits!
    

    这里的正则表达式是\d{10}\d 表示您正在寻找一个数字,{10} 表示您需要其中的十个。 ^ 标记字符串的开头,$ 标记字符串的结尾。

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

    【讨论】:

      猜你喜欢
      • 2017-04-07
      • 2020-07-22
      • 2022-09-28
      • 1970-01-01
      • 2011-07-20
      • 2011-06-15
      • 2012-11-02
      相关资源
      最近更新 更多