流程控制(重点)
# 控制事物的执行流程
流程控制总共有3种情况:
1. 顺序结构
# 就是自上而下的执行
2. 分支结构
# 分支结构就是根据条件判断的真假去执行不同分支对应的子代码
3. 循环结构
# 循环结构就是重复执行某段代码块
分支结构
"""
注意事项:
1. 根据条件的成立与否,决定是否执行if代码块
2. 我们通过缩进代码块,来表示代码之间的从属关系
3. 不是所有的代码都拥有子代码块
4. 我们推荐使用缩进4格
5. 同属于一个代码块的子代码块缩进量一定要一样
ps:遇到冒号就要回车换行,缩进
"""
# 1. 单if判断
关键字:if
"""
语法格式:
if 判断条件:
print('小姐姐好')
"""
# 2. 双分支结构
"""
语法格式:
if 判断条件:
条件成立执行的子代码块
else:
条件不成立执行的子代码块
"""
# 3. 多分支结构
"""
语法格式:
if 条件1:
条件1成立执行的子代码块
elif 条件2:
条件1不成立条件2成立执行的子代码块
elif 条件3:
条件1、2不成立条件3成立执行的子代码块
elif 条件4:
条件1、2、3不成立条件4成立执行的子代码块
else:
以上条件都不成立的时候执行的代码块
"""
# else语句是可有可无的
score = 56
if score >= 90:
print('优秀')
elif score >=80:
print('良好')
elif score >=70:
print('一般')
elif score >=60:
print('及格')
if判断之嵌套
age = 18
height = 1.6
weight = 90
is_beautiful=True
is_success=True
if age < 20 and height == 1.6 and weight<100 and is_beautiful:
print('给个微信呗!!!')
'''要么给,要么不给'''
if is_success:
print('吃饭 看电影...')
else:
print('慢走不送!!!')
else:
print('好吧')
循环结构while
"""
while语法格式
while 条件:
循环体
"""
while True:
username=input('username:>>>')
password=input('password:>>>') #
if username == 'kevin' and password == '123':
print('登录成功')
else:
print('登录失败')
while+break
# count = 0
while True:
username=input('username:>>>')
password=input('password:>>>') #
if username == 'kevin' and password == '123':
print('登录成功')
break # 结束本层循环
else:
print('登录失败')
break本层含义
while True:
username=input('username:>>>')
password=input('password:>>>') #
if username == 'kevin' and password == '123':
print('欢迎光临')
while True:
cmd=input('请输入你的指令:>>>')
if cmd == 'q':
# 结束程序
break
print('正在执行你的指令:%s' % cmd)
break
else:
print('登录失败')
print(123)
标志位的使用
flag = True
while flag:
username=input('username:>>>')
password=input('password:>>>') #
if username == 'kevin' and password == '123':
print('欢迎光临')
while flag:
cmd=input('请输入你的指令:>>>')
if cmd == 'q':
# 结束程序
flag = False
print('正在执行你的指令:%s' % cmd)
else:
print('登录失败')