编程小白的第一本Python入门书学习笔记 Chaper5:循环与判断

逻辑控制与循环

逻辑判断 – True / Flase

上面的TrueFalse称为布尔型数据,凡能够产生一个布尔值的表达式成为布尔表达式,通过以下布尔表达式体会布尔值的用法。

print(1 > 2)
False
print(1 < 2 < 3)
True
print(42 != '42')
True
print('Name' == 'name')
False
print('M' in 'Magic')
True
number = 12
print(number is 12)
True

比较运算

比较运算符: ==!=><<=>=
除简单、直接的比较外,比较运算还支持更丰富的表达方式

# 多条件比较
middle = 5
1 < middle < 10
True
# 变量的比较
two = 1 + 1
three = 1 +2
two < three
True
# 字符串的比较,即比较两个字符串是否完全一致,注:python严格区分大小写
'Eddie Van Helen' == 'eddie van helen'
False
# 两个函数结果的比较
abs(-10) > len('length of this word')
False
比较运算的两个小问题
  1. 不同类型的对象不能使用"<, >, <=, >=“比较,但可以使用”==“和”!="
42 > 'the answer' # 不能比较,报错
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-11-d93c2e59925c> in <module>
----> 1 42 > 'the answer' # 不能比较,报错


TypeError: '>' not supported between instances of 'int' and 'str'
42 == 'the answer' # 可以比较
False
42 != 'the answer' # 可以比较
True
  1. 虽然浮点型和整型是不同类型,但是不影响比较运算
5.0 == 5
True
3.0 > 1
True
布尔型数据的比较
True > False
True
True + False > False + False
True

TrueFalse对于计算机就等价于10,所以上面两个表达式等价于:

1 > 0
True
1 + 0 > 0 + 0
True

另注:1 <> 3 等价于 1 != 3,写法不同而已

在python中任何对象的都可判断其布尔值,除了0None,和所有空的序列与集合(列表、字典、集合)的布尔值为Flase之外,其他都为True可以使用bool()函数进行判别

bool(0)
False
bool([])
False
bool('')
False
bool(False)
False
bool(None)
False

成员运算符in和身份运算符is

成员运算符和身份运算符的关键词是inis

  • in 的含义是:测试前者是否存在于in后面的集合中,与not in对应;
  • is是进行身份对比的,其含义是:经过is对比,两个变量一致时则返回True,否则返回False,与is not对应;
  • innot in是表示归属型关系的运算,isnot is是表示身份鉴别的运算

预备知识:简单了解一下一个简单的集合类型,列表的概念

  • 列表是一个简单、实用的集合类型
  • 字符串、浮点型、整型、布尔型数据、变量和另一个列表都可以储存在列表中
  • 和创建变量一样,创建列表需要先给它起一个名字
  • 列表可以是空的,也可以是非空的
# 空列表
album = []
# 非空列表
album = ['Black Star', 'David Bowie', 25, True]
print(album)
['Black Star', 'David Bowie', 25, True]

当列表创建完成后,想再次往里添加内容时,使用列表的方法:append ,向列表中添加新元素。使用这种方式添加的元素自动排列到列表尾部

album.append('new song')
album
['Black Star', 'David Bowie', 25, True, 'new song']

列表的索引,与字符串的索引类似

# 打印列表中的第一个元素和最后一个元素
print(album[0], album[-1])
Black Star new song

进入正题

  • 使用in测试字符串 ‘Black Star’ 是否在列表album中,如果存在返回True, 否则返回False
print('Black Star' in album)
print("Black Star" not in album)
True
False
  • 使用is测试the_Eddiename两个变量是否相等,如果相等返回True, 否则返回False
the_Eddie = 'Eddie'
name = 'Eddie'
print(the_Eddie == name)
print(the_Eddie is name)
print(the_Eddie is not name)
True
True
False

布尔运算符:not, and, or

与逻辑运算符相对应,not是取非,and是与运算,or是或运算

运算符描述
not x如果 x 是 True,则返回 False,否则返回 True
x and yand 表示“并且”,如果 x 和 y 都是 True,则返回True;如果 x 和 y 有一个是 False,则返回False
x or yor 表示“或者”,如果 x 或 y 其中有一个是 True,则返回True;如果 x 和 y 都是False,则返回False
# 非运算
print(not bool(None))
True

andor经常用于处理复合条件,类似于1 < n < 3, 也就是两个条件同时满足

print(1 < 3 and 2 < 5)
print(1 < 3 and 2 > 5)
print(1 < 3 or 2 > 5)
print(1 > 3 or 2 > 5)
True
False
True
False

条件控制

简单条件判断

# 伪代码
if condition:
    do something 1
else:
    do something 2

if, else关键字;condition成立的条件;
if...else结构的作用:如果if后条件成立,即返回True则做do something 1, 否则做else后的do something 2

# 例子:账号登录
# 定义函数
def account_login():
    password = input('Password:')
    if password == '12345':
        print('Login Success!')
    else:
        print('Wrong password or invalid input!')
        account_login() # 如果输入错误,则再次运行函数,让用户重新输入;如果不加这句就只输入一次,然后打印

# 调用函数 
account_login()
Password: 666
Wrong password or invalid input!
Password: 12345
Login Success!

如果if后的布尔表达式过长或者难于理解,可以采用给变量赋值的方法来储存布尔表达式返回的布尔值,所以更清晰的写法:

# 定义函数
def account_login():
    password = input('Password:')
    password_correct = password == '12345' # 这里给变量赋值以便更好地理解 if 后 condition 的含义
    if password_correct:
        print('Login Success!')
    else:
        print('Wrong password or invalid input!')
        account_login()

# 调用函数
account_login()
Password: 666
Wrong password or invalid input!
Password: 12345
Login Success!

多条件判断

  • ifelse之间加上elif,用法和if一致,条件判断依次进行,若都不成立则运行else对应的语句
  • elif可以有无限多个
  • 也可以不用else,都用elif
# 伪代码
if condition_1:
    do something 1
elif condition_2:
    do something 2 
else:
    do something 3    

给前面的函数增加一个密码重置的功能

# 创建一个列表,用于储存用户的密码、初始密码和其他数据(对实际数据库进行简化模拟)
password_list = ['*#*#', '12345']

# 定义函数
def account_login():
    password = input('Please input your password:')
    # 当用户输入密码等于列表中最后一个元素(即用户设定的密码),登录成功
    password_correct = password == password_list[-1]
    # 当用户输入密码等于列表中第一个元素时(即重置密码“口令”)触发密码变更,并将变更密码储存至列表的最后一个,成为用户新密码
    password_reset = password == password_list[0]
    if password_correct: # 密码正确
        print('Login Success!')
    elif password_reset: # 密码重置
        new_password = input('Please enter your new password:')
        password_list.append(new_password)
        # 注意上面这句不要漏了,因为密码列表没有变,所以不添加的话密码还是'12345',相当于没有改密码
        # 所以需要将新密码添加至password_list中,append默认添加至最后一位
        print('Your password has changed successfully!')
        account_login()
    else: # 若上述情况均不成立,则打印错误提示,并再次运行函数,让用户继续输入密码
        print('Wrong password or invalid input!')
        account_login()
        
# 调用函数
account_login()
Please input your password: 666
Wrong password or invalid input!
Please input your password: *#*#
Please enter your new password: 666
Your password has changed successfully!
Please input your password: 666
Login Success!

再次强调:

  1. 所有关键字后面的冒号一定不能少,否则会报语法错误;
  2. 一定要注意缩进,相同缩进的代码块完成的是一个层级的事儿,有点像编辑文档时不同层级的任务列表

循环

for 循环

for循环所做的事情概括来讲就是:对集合:iterable中的每一个元素item,做do something

# 伪代码
for item in iterable:
    do something 

for, in是关键字;item是指集合中的每一个元素;iterable是指可以连续地提供每一个迭代元素的对象,是一个集合形态的对象;
do something是指迭代过程中每一个元素需要做的事;
再次强调:注意冒号和缩进

小例子:打印出'hello world'这个字符串中的每一个字符

for every_letter in 'Hello world':
    print(every_letter)
H
e
l
l
o
 
w
o
r
l
d

再来一个小例子,继续理解for循环

for num in range(1,11):
    print(str(num) + '+1 =', num + 1)
    # 打印的另一种写法
    # print(num, '+1=', num+1)
1+1 = 2
2+1 = 3
3+1 = 4
4+1 = 5
5+1 = 6
6+1 = 7
7+1 = 8
8+1 = 9
9+1 = 10
10+1 = 11

进阶小例子:将forif组合起来用
歌曲列表中有三首歌分别是“Holy Diver, Thunderstruck, Rebel Rebel”,当播放到每首歌曲时,分别显示对应歌手的名字“Dio, AC/DC, David Bowie”
实现过程:将SongsList列表中的每一个元素依次取出,分别与三个条件比较,成立则输出对应的内容。

SongsList = ['Holy Diver', 'Thunderstruck', 'Rebel Rebel']
for song in SongsList:
    if song == 'Holy Diver':
        print(song, '- Dio')
    elif song == 'Thunderstruck':
        print(song, '- AC/DC')
    elif song == 'Rebel Rebel':
        print(song, '- David Bowie')
Holy Diver - Dio
Thunderstruck - AC/DC
Rebel Rebel - David Bowie
嵌套循环

小例子:九九乘法表

for i in range(1, 10):
    for j in range(1, 10):
        print('{} × {} = {}'.format(i, j, i*j))
1 × 1 = 1
1 × 2 = 2
1 × 3 = 3
1 × 4 = 4
1 × 5 = 5
1 × 6 = 6
1 × 7 = 7
1 × 8 = 8
1 × 9 = 9
2 × 1 = 2
2 × 2 = 4
2 × 3 = 6
2 × 4 = 8
2 × 5 = 10
2 × 6 = 12
2 × 7 = 14
2 × 8 = 16
2 × 9 = 18
3 × 1 = 3
3 × 2 = 6
3 × 3 = 9
3 × 4 = 12
3 × 5 = 15
3 × 6 = 18
3 × 7 = 21
3 × 8 = 24
3 × 9 = 27
4 × 1 = 4
4 × 2 = 8
4 × 3 = 12
4 × 4 = 16
4 × 5 = 20
4 × 6 = 24
4 × 7 = 28
4 × 8 = 32
4 × 9 = 36
5 × 1 = 5
5 × 2 = 10
5 × 3 = 15
5 × 4 = 20
5 × 5 = 25
5 × 6 = 30
5 × 7 = 35
5 × 8 = 40
5 × 9 = 45
6 × 1 = 6
6 × 2 = 12
6 × 3 = 18
6 × 4 = 24
6 × 5 = 30
6 × 6 = 36
6 × 7 = 42
6 × 8 = 48
6 × 9 = 54
7 × 1 = 7
7 × 2 = 14
7 × 3 = 21
7 × 4 = 28
7 × 5 = 35
7 × 6 = 42
7 × 7 = 49
7 × 8 = 56
7 × 9 = 63
8 × 1 = 8
8 × 2 = 16
8 × 3 = 24
8 × 4 = 32
8 × 5 = 40
8 × 6 = 48
8 × 7 = 56
8 × 8 = 64
8 × 9 = 72
9 × 1 = 9
9 × 2 = 18
9 × 3 = 27
9 × 4 = 36
9 × 5 = 45
9 × 6 = 54
9 × 7 = 63
9 × 8 = 72
9 × 9 = 81

while 循环

while循环所做的事概括来讲就是:只要condition条件成立,就一直do something

# 伪代码
while condition:
    do something

while是关键字;condition是成立的条件;do something是想要做的事

一个简单的例子(注意:这个例子是一个死循环,要及时停止代码,因为当while后条件成立时就会一直执行代码)

# while 1 < 3:
#     print('1 is smaller than 3') 

那么应该如何控制while循环呢?一种方式是:在循环过程中制造某种可以使循环停下来的条件,正如下面的例子所示:

count = 0
while True:
    print('Repeat this line !')
    count = count + 1
    if count == 5:
        break # 若条件成立则终止循环,是终止循环的信号
Repeat this line !
Repeat this line !
Repeat this line !
Repeat this line !
Repeat this line !

while循环停下来的另一种方法是:改变循环成立的条件,用例子说明:给前面的登录函数增加一个新功能,当输入错误密码超过三次后就禁止再次输入密码。

# 创建一个列表,用于储存用户的密码、初始密码和其他数据(对实际数据库进行简化模拟)
password_list = ['*#*#', '12345']

# 定义函数
def account_login():
    tries = 3
    while tries > 0:
        password = input('Please input your password:')
        # 当用户啊输入密码等于列表中最后一个元素(即用户设定的密码),则登录成功
        password_correct = password == password_list[-1]
        # 当用户输入密码等于列表中第一个元素时(即重置密码“口令”)则触发密码变更,并将变更的新密码储存至列表的最后一个,成为用户的新密码
        password_reset = password == password_list[0]
        if password_correct: # 密码正确
            print('Login Success!')
            break # 终止循环
        elif password_reset: # 密码重置
            new_password = input('Please enter your new password:')
            password_list.append(new_password)
            # 注意上面这句不要漏了,因为密码列表没有变,所以不添加的话密码还是'12345'
            # 所以需要将新密码添加至password_list中,append默认添加至最后一位
            print('Your password has changed successfully!')
            # 这里不需要重新调用函数,因为是要自动循环的,调用函数反而回到起点了,不能终止

        else: # 若上述情况均不成立,则打印错误提示,并再次运行函数,让用户输入密码
            print('Wrong password or invalid input!')
            tries = tries - 1 # 当密码输入错误时,尝试机会减 1,并提示
            # 这里也不需要调用,原理同上
            print(tries, 'times left')
    else: # 这里的else是和while对应的,是while--else结构
        print('Your account has been suspended')
        
        
# 调用函数
account_login()
Please input your password: 666
Wrong password or invalid input!
2 times left
Please input your password: 365    
Wrong password or invalid input!
1 times left    
Please input your password: 125    
Wrong password or invalid input!
0 times left
Your account has been suspended

练习题

  1. 设计一个这样的函数,在桌面的文件夹创建10个文本,以数字给它们命名
  2. 复利是一件神奇的事情,正如富兰克林所说:“复利是能够将所有铅块变成金块的石头”。设计一个复利函数invest(), 它包含三个参数:
    account(资金),rate(利率),time(投资时间)。输入每个参数后调用函数,应该返回每一年的资金总额。(假设利率为5%)
  3. 打印1-100内的偶数
# 第 1 题 ,第一次未写出,参考答案
def text_creation():
    for name in range(1,11):
        desktop_path = 'C:/Users/username/Desktop/'
        full_path = desktop_path + str(name) + '.txt'
        file = open(full_path,'w')
        file.close()
    
text_creation()
# 第 1 题,第二次做
# 定义函数
def create_text():
    for num in range(1, 11):
        path = '/Users/username/Desktop/' + str(num) + '.txt'
        file = open(path, 'w')
        file.close()
        # 答案中open的写法是:
        # with open(path) as text:
        #     text.close()

# 调用函数
create_text()
# 第 2 题,第一次未写出,参考答案
def invest(amount, rate, time):
    print("principal amount:{}".format(amount))
    for t in range(1, time + 1):
        amount = amount * (1 + rate)# 复利计算:当时的本金乘以(1+利率)
        print("year {}: ${}".format(t, amount))

# 调用
invest(100, 0.05, 8)
invest(2000, 0.025, 5)
principal amount:100
year 1: $105.0
year 2: $110.25
year 3: $115.7625
year 4: $121.55062500000001
year 5: $127.62815625000002
year 6: $134.00956406250003
year 7: $140.71004226562505
year 8: $147.74554437890632
principal amount:2000
year 1: $2050.0
year 2: $2101.25
year 3: $2153.78125
year 4: $2207.62578125
year 5: $2262.8164257812496
# 第 2 题,第二次做
# 假设利率为5%
def invest(amount, time, rate=0.05):
    print('principal amount:', amount)
    for year in range(1, time+1):
        # 复利的计算
        amount = amount + amount * rate
        print('year ', year, ': $', amount)

# 调用函数
invest(100, 8)
principal amount: 100
year  1 : $ 105.0
year  2 : $ 110.25
year  3 : $ 115.7625
year  4 : $ 121.550625
year  5 : $ 127.62815624999999
year  6 : $ 134.00956406249998
year  7 : $ 140.71004226562496
year  8 : $ 147.7455443789062
# 第 3 题,第一次做
for num in range(2, 101, 2):
    print(num)
# 第 3 题,第二次做
def even_print():
    for i in range(1, 101):
        if i % 2 == 0: # %是取余运算,//是取整除,即向下取整
            print(i)
# 调用函数
even_print()
# 第 3 题,参考答案
def even_print():
    for i in range(1,101):
        if i%2 == 0: # 能被2整除的数就是偶数
            print(i)
            
even_print()

综合练习

预备知识:

  1. 对于一个数字列表,使用sum()函数可以对列表中所有的整数求和,如下例所示。
a_list = [1, 2, 3]
print(sum(a_list))
6
  1. Python中的random内置库可以用于生成随机数,此外random中的randrange()方法可以限制生成随机数的范围,如下例所示。
# 导入 random 内置库,用它生成随机数
import random 

point1 = random.randrange(1,7)
point2 = random.randrange(1,7)
point3 = random.randrange(1,7)

print(point1, point2, point3)
5 5 3

游戏开始:押大小,即选择完成后开始摇三个骰子,计算总值。11 <= 总值 <= 18 为“大”,3 <= 总值 <= 10 为“小”,然后告诉玩家猜对或者猜错的结果

import random 

point1 = random.randrange(1,7)
point2 = random.randrange(1,7)
point3 = random.randrange(1,7)

a_list = [point1, point2, point3]
result = sum(a_list)
guess = input('Big or Small: ')
if 3 <= result <= 10 and guess == 'Big':
    print('The points are {}'.format(a_list), 'You Lose!')
elif 3 <= result <= 10 and guess == 'Small':
    print('The points are {}'.format(a_list), 'You Win!')
elif 11 <= result <= 18 and guess == 'Big':
    print('The points are {}'.format(a_list), 'You Win!')
elif 11 <= result <= 18 and guess == 'Small':
    print('The points are {}'.format(a_list), 'You Lose!')
Big or Small:  Big    
The points are [6, 6, 2] You Win!
# 参考答案
# 首先构造摇骰子函数 roll_dice, 调用后返回含三个点数结果的列表
import random 
def roll_dice(numbers = 3, points = None):
    print('<<<<<  ROLL THE DICE!  >>>>>')
    if points is None:
        points = []
    while numbers > 0:
        point = random.randrange(1,7)
        points.append(point)
        numbers = numbers - 1
    return points 

# 创建函数:将点数转化为大小,并使用 if 语句来定义什么是“大”,什么是“小”
def roll_result(total):
    isBig = 11 <= total <= 18
    isSmall = 3 <= total <= 10
    if isBig:
        return 'Big'
    elif isSmall:
        return 'Small'
    
# 创建一个开始游戏函数,让用户输入猜大小,并定义什么是猜对,什么是猜错,并输出对应的输赢结果
def start_game():
    print('<<<<<  GAME STARTS!  >>>>>')
    choices = ['Big', 'Small']
    your_choice = input('Big or Small: ')
    if your_choice in choices:
        points = roll_dice()
        total = sum(points)
        youWin = your_choice == roll_result(total)
        if youWin:
            print('The points are ', points, 'You Win !')
        else:
            print('The points are ', points, 'You lose !')
    else:
        print('Invaild Words')
        start_game()

start_game()
    
<<<<<  GAME STARTS!  >>>>>    
Big or Small:  Big    
<<<<<  ROLL THE DICE!  >>>>>
The points are  [3, 1, 5] You lose !    

练习题

  1. 较难 在最后一个项目的基础上增加这样的功能,下注金额和赔率。具体规则如下:
  • 初始金额为100元;
  • 金额为0时游戏结束;
  • 默认赔率为1倍,也就是说押对了能得相应金额,押错了会输掉相应金额;
  1. 我们注册应用上时候,常常使用手机号作为账户名,在短信验证之前一般都会检验号码的真实性,如果时不存在的号码就不会发送验证码,
    检验规则如下:
  • 长度不少于11位;
  • 是移动,联通,电信号段中的一个电话号码;
  • 移动号段,联通号段,电信号段如下:
    CN_mobile = [134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705]
    CN_union = [130,131,132,155,156,185,186,145,176,1709]
    CN_telecom = [133,153,180,181,189,177,1700]
# 第 1 题,结合参考答案 新概念:全局变量
import random
# 设置moneyAll为全局变量,注意:哪里需要全局变量,哪里声明一下;
global moneyAll
moneyAll = 100

def roll_dice(numbers=3):
    print('<<<<< ROLL THE DICE! >>>>>')
    points=[]
    while numbers>0:
        point=random.randrange(1,7)
        points.append(point)
        numbers=numbers-1
    return points

def roll_result(total):
    isBig=11<=total<=18
    isSmall=3<=total<=10
    if isBig:
        return 'Big'
    elif isSmall:
        return  'Small'

def start_game():
    # 这个需要使用到上面设置的全局变量,所以要在这重新声明
    global moneyAll
    print('<<<<< GAME STARTS! >>>>>')
    choices=['Big','Small']
    your_choice=input('Big or Ssmall:')
    if your_choice in choices:
        # input()输入的是字符串,要使用数字运算、比较需要转为int型
        your_bet = int(input('How much you wanna bet?-'))
        if moneyAll >= your_bet:
            points = roll_dice()
            total = sum(points)
            youWin = your_choice == roll_result(total)
            if youWin:
                print('The points are',points,'You win !')
                moneyAll += your_bet
                print('You gained',your_bet,'You have',moneyAll,'now')
                start_game()
            else:
                print('The points are', points, 'You lose !')
                moneyAll -= your_bet
                print('You lost', your_bet, 'You have', moneyAll, 'now')
                if moneyAll == 0:
                    print('GAME OVER')
                else:
                    start_game()
        else:
            print('not full money')
            start_game()
    else:
        print('Invalid Words')
        start_game()

start_game()
<<<<< GAME STARTS! >>>>>
Big or Ssmall: Big
How much you wanna bet?- 150    
not full money
<<<<< GAME STARTS! >>>>>    
Big or Ssmall: Big
How much you wanna bet?- 80    
<<<<< ROLL THE DICE! >>>>>
The points are [3, 1, 4] You lose !
You lost 80 You have 20 now
<<<<< GAME STARTS! >>>>>    
Big or Ssmall: Big
How much you wanna bet?- 30    
not full money
<<<<< GAME STARTS! >>>>>    
Big or Ssmall: Big
How much you wanna bet?- 20    
<<<<< ROLL THE DICE! >>>>>
The points are [1, 3, 5] You lose !
You lost 20 You have 0 now
GAME OVER
# 第 2 题
def num_verification():
    CN_mobile = [134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705]
    CN_union = [130,131,132,155,156,185,186,145,176,1709]
    CN_telecom = [133,153,180,181,189,177,1700]
    number = input('Please enter your number:')
    num_length = len(number)
    # input()的输入是srt型,在这儿切片时需要用到的是int型,所以需要转换数据类型
    # 为什么不在上面转换,因为转换后不能求len()
    # 切片的索引是从0开始的,左闭右开
    first_three = int(number[:3])
    first_four = int(number[:4])
    if num_length == 11:
        if first_three in CN_mobile or first_three in CN_union or first_three in CN_telecom:
            print('Sending verification code to your phone:', number)
        elif first_four in CN_mobile or first_four in CN_union or first_four in CN_telecom:
            print('Sending verification code to your phone:', number)
        else:
            print('Invalid phone number')
            num_verification()
    else:
        print('Invalid phone number')
        num_verification()
        
num_verification()
Please enter your number: 236548    
Invalid phone number    
Please enter your number: 36578965214    
Invalid phone number    
Please enter your number: 13025647895    
Sending verification code to your phone: 13025647895
# 第 2 题,参考答案
def chephone():
    phoneNumber=input('Enter Your number:')
    numlenth=len(phoneNumber)
    CN_mobile = [134, 135, 136, 137, 138, 139, 150, 151, 152, 157, 158, 159, 182, 183, 184, 187, 188, 147, 178, 1705]
    CN_union = [130, 131, 132, 155, 156, 185, 186, 145, 176, 1709]
    CN_telecom = [133, 153, 180, 181, 189, 177, 1700]
    first_three=int(phoneNumber[0:3])
    first_four=int(phoneNumber[0:4])
    if numlenth!=11:
        print('Invalid length,your number should be in 11 digits')
        chephone()
    elif first_three in CN_mobile or first_four in CN_mobile:
        print('Opetator:China Mobile')
        print('We\'re sending verification code via text to your phone:',phoneNumber)
    elif first_three in CN_union or first_four in CN_union:
        print('Opetator:China Union')
        print('We\'re sending verification code via text to your phone:', phoneNumber)
    elif first_three in CN_telecom or first_four in CN_telecom:
        print('Opetator:China Telecom')
        print('We\'re sending verification code via text to your phone:', phoneNumber)

    else:
        print('No such a operator')
        chephone()

chephone()

Enter Your number: 698546   
Invalid length,your number should be in 11 digits    
Enter Your number: 32569874563    
No such a operator    
Enter Your number: 13256987456    
Opetator:China Union
We're sending verification code via text to your phone: 13256987456
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值