【问题标题】:Python: Trying to validate input such as a phone number to ensure it is 10 numerical characters and prompt and error if notPython:尝试验证电话号码等输入以确保它是 10 个数字字符,如果不是则提示和错误
【发布时间】:2013-04-20 22:44:17
【问题描述】:

我希望程序删除 () 和 - 可能会在有人输入他们的电话时输入。如果不产生循环,我还想确保它是 10 个数字字符长。

p = raw_input("Please enter your 10 digit Phone Number")
def only_numerics(p):
    seq_type= type(p)
    return seq_type().join(filter(seq_type.isdigit, p))
p = only_numerics(p)

valid_phone = False
while not valid_phone:

    if p > "0000000000" and p < "9999999999" and len(p) == 10 :
        print "You have entered " + p
        valid_phone=True
    else:
        print "You have entered an invalid choice"

如果我输入少于 10 个数字,我会重复 else 打印命令。我希望它返回原始输入(“请输入您的 10 位电话号码”)。有没有办法做到这一点?

【问题讨论】:

  • 是的,在整个代码块周围放置一个while 循环。如果!valid_phonewhile 循环应该重复
  • 旁注:你可以使用比较链来代替and,而不是"0000000000" &lt; p "9999999999"

标签: python loops validation


【解决方案1】:

除了像 James 指出的那样在循环内设置“raw_input”之外,您可能对使用 regular expressions 并让您的代码更漂亮感兴趣:

import re
phone_re = re.compile(r'\d{10}$')

def only_numerics(p):
    seq_type= type(p)
    return seq_type().join(filter(seq_type.isdigit, p))

valid_phone = False
while not valid_phone:
    p = raw_input("Please enter your 10 digit Phone Number: ")
    p = only_numerics(p)
    if phone_re.match(p):
        print "You have entered " + p
        valid_phone=True
    else:
        print "You have entered an invalid choice"

【讨论】:

    【解决方案2】:

    它循环回到 print 语句,因为您在 while 循环之外定义了 p。更改此设置将解决循环问题:

    valid_phone = False
    while not valid_phone:
    
        p = raw_input("Please enter your 10 digit Phone Number")
        def only_numerics(p):
            seq_type= type(p)
            return seq_type().join(filter(seq_type.isdigit, p))
        p = only_numerics(p)
    
    
        if p > "0000000000" and p < "9999999999" and len(p) == 10 :
            print "You have entered " + p
            valid_phone=True
        else:
            print "You have entered an invalid choice"
    

    【讨论】:

    • 你不能像那样比较字符串;转换为 int 将允许您比较
    • 我已经测试了代码,它工作得很好,除了它使用 OP 的代码来显示代码结构中的缺陷的简单答案
    【解决方案3】:

    def val_number(input_num):

    valid_num=re.findall(r'\d+',input_num)
    if len(valid_num[0])>10:
        print('You have entered more than 10 digits')
    else:
        return(valid_num)
    

    【讨论】:

      【解决方案4】:

      此 python 代码验证 11 位手机号码:

      valid = True
      mobile_number = str(input("What is your mobile number? "))
      while len(mobile_number) != 11 or mobile_number[0] != "0" or mobile_number[1:].isdigit() == False:
          mobile_number = str(input("Please enter a valid mobile number: "))
          valid = False
      print("Your mobile number is valid")
      

      【讨论】:

        【解决方案5】:

        当(真):

        try:
            phone_number=int(input("please enter the phone number:no spaces in between: \n"))
        except ValueError:
            print("mismatch")
            continue
        else:
            phone=str(phone_number)
            if(len(phone)==10):
                break
            else:
                print("10 digits please ")
                continue
        

        【讨论】:

          猜你喜欢
          • 2017-10-15
          • 2018-05-07
          • 1970-01-01
          • 1970-01-01
          • 2020-04-14
          • 1970-01-01
          • 2021-05-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多