京东的注册页面,打开页面我们就看到这些要求输入个人信息的提示。
假如我们随意的在手机号码这一栏输入一个11111111111,它会提示我们格式有误。
这个功能是怎么实现的呢?
假如现在你用python写一段代码,类似:

phone_number = input('please input your phone number : ')

你怎么判断这个phone_number是合法的呢?

根据手机号码一共11位并且是只以13、14、15、18开头的数字这些特点,我们用python写了如下代码:
while True:
    phone_number = input('please input your phone number : ')
    if len(phone_number) == 11 \
            and phone_number.isdigit()\
            and (phone_number.startswith('13') \
            or phone_number.startswith('14') \
            or phone_number.startswith('15') \
            or phone_number.startswith('18')):
        print('是合法的手机号码')
    else:
        print('不是合法的手机号码')
low版

相关文章:

  • 2021-12-19
  • 2021-07-30
  • 2021-11-05
  • 2021-09-04
  • 2022-01-12
  • 2021-10-21
  • 2022-12-23
猜你喜欢
  • 2021-12-18
  • 2021-12-26
  • 2021-05-14
  • 2021-08-13
  • 2021-12-09
  • 2022-01-02
  • 2021-08-23
相关资源
相似解决方案