10月 212020
 
# -*- coding:utf-8 -*-
def number_test():  # 定义函数,测试号码所属运营商
    while True:  # 循环提示用户输入号码,在输入长度不匹配或输入号码匹配运营商号段列表时终止循环
        number = input('Enter your number: ')  # 用户输入赋值变量
        china_mobile = [134, 135, 136, 137, 138, 139, 150, 151, 152,
                        157, 158, 159, 182, 183, 184, 187, 188, 147, 178, 1705]
        china_unicom = [130, 131, 132, 155, 156, 185, 186, 145, 176, 1709]
        china_telecom = [133, 153, 180, 181, 189, 177, 1700]
        first_three = int(number[0:3])  # 取用户输入字符串第1-3位转换整数类型并赋值变量
        first_four = int(number[0:4])  # 取用户输入字符串第1-4位转换整数类型并赋值变量

        if len(number) == 11:  # 判断用户输入长度是否为11位

            if first_three in china_mobile or first_four in china_mobile:  # 判断用户输入前三位或前四位是否在移动号段列表中
                print('Operator: China Mobile')
                print('We\'re sending verification code via text to your phone: ', number)
                break  # 匹配则终止判断
            elif first_three in china_telecom or first_four in china_telecom:  # 如果不在移动号段列表,继续判断用户输入前三位是否在电信号段列表中
                print('Operator: China Telecom')
                print('We\'re sending verification code via text to your phone: ', number)
                break  # 匹配则终止判断
            elif first_three in china_unicom or first_four in china_unicom:  # 如果不在移动号段和电信号段列表,继续判断用户输入前三位是否在联通号段列表中
                print('Operator: China Unicom')
                print('We\'re sending verification code via text to your phone: ', number)
                break  # 匹配则终止判断
            else:  # 用户输入不存在于号段列表中则提示没有此运营商
                print('No such a operator')

        else:  # 反之,则提示用户输入长度不足
            print('Invalid length, your number should be in 11 digits')


# 调用函数
number_test()

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据