流程控制即控制程序的执行流程
程序的执行流程分为三种结构:1、顺序结构
2、分支结构(if语句)
3、循环结构(while、for语句)
分支结构,if:主要用于判断事物对错,是否可行。
语法结构:
if 条件1: 代码块1 ... elif 条件2: 代码块2 elif 条件3: 代码块3 ...... else: 代码块n
if...elif...else:同一个代码结构里只会执行一个,例如执行if就不会执行elif和else。
案例1:
gender = 'female' age = 18 is_beauty = True if gender =='female' and 16 < age < 26 and is_beauty: print('Wow') elif 16 < age < 36 and is_beauty: print('Hah') else: print('Emm..')
案例2 模拟认证功能:1、接收用户的输入
2、判断用户的输入结果
3、返回数据
username = 'Bill' password = '123' inp_username = input('input your username:') inp_password = input('input your password:') if inp_username == username and inp_password == password: print('登录成功') else: print('登录失败')
if嵌套:
gender = 'female' age = 18 is_beauty = True is_success = True if gender =='female' and 16 < age < 26 and is_beauty: print('Wow') if is_success: print('date') else: print('gun') elif 16 < age < 36 and is_beauty: print('Hah') else: print('Emm..')
循环结构,while 语法结构:
while 条件:
代码块
while中可加break、continue、else:
break:结束当层循环
continue:跳过本次循环,执行下一次循环;Continue下不管有多少代码都不会执行
else:当while循环正常执行结束时就会执行else的代码块(可以用来检查循环是否正常)
案例:给登录功能设置错误登录上限
username = 'Bill'
password = '123'
count = 0
while count < 3:
inp_username = input('input your username:')
inp_password = input('input your password:')
if inp_username == username and inp_password == password:
print('登录成功')
break # 若不加break 登录成功后会再一次循环
else:
print('登录失败')
count += 1
while循环嵌套:
username = 'Bill' password = '123' count = 0 while count < 3: inp_username = input('input your username:') inp_password = input('input your password:') if inp_username == username and inp_password == password: print('登录成功') while True: cmd = input('执行命令') if cmd == 'exit': break else: print('run<%s>' % cmd) break # 若不加break 登录成功后会再一次循环 else: print('登录失败') count += 1
if count == 3:
print('locked')
break
while循环嵌套+tag:
username = 'Bill' password = '123' count = 0 tag = True while tag: inp_username = input('input your username:') inp_password = input('input your password:') if inp_username == username and inp_password == password: print('登录成功') while tag: cmd = input('执行命令') if cmd == 'exit': tag = False break print('run<%s>' % cmd) else: print('登录失败') count += 1 if count == 3: print('locked') break
while嵌套+continue
Num = 11 while Num > 1: Num-=1 if Num%7 == 0: continue# 结束本次循环,本次循环continue之后的代码都不运行,直接进入下一次循环 print(Num)
while+else
count = 0 while count < 10: count+=1 print('Loop',count) else: print('撸完了') print('out of while loop'.center(30,'*')) # 当while循环执行完成并且没有被break语句中断时,就会执行else后的语句,可以用else语句来验证while循环正常结束。 count = 0 while count < 10: count+=1 if count == 5: break # 如果执行中被break,则不会进行else语句 print('Loop',count) else: print('撸完啦') print('out of while loop'.center(30,'*'))
for循环:
for 变量名 in 容器类型(可迭代对象)
代码1
代码2
...
break、continue、else也能用于for循环,使用语法同while循环。
案例:
List = [1,2,3,4,5,6] for Num in List: print(Num) Dict = {'name':'Bill','age':25,'sex':'male'} for info in Dict: print(info) # 遍历字典
for循环嵌套:
for i in range(3): for j in range(5): print('*', end='') print()
案例,乘法表:
for i in range(1, 10): for j in range(1, i+1): print(f'{i}*{j}={i*j}', end=' ') print()