03 python开发之流程控制
[TOC]
3 流程控制
3.1 流程判断之if判断
3.1.1 代码块
-
代码块指同一级别的代码在python中使用相同缩进的空格数
-
顶级代码块无任何缩进,其余代码都是在原有基础上缩进4个空格来标识同一级别的代码块
-
同一级别的代码块会按照自上而下的顺序依次执行
3.1.2 if判断基础语法
# 基础语法if 条件1:代码1代码2代码3elif 条件2:......else:...# 单分支if 条件:代码1代码2代码3# 双分支if 条件1:代码1代码2代码3elif 条件2:......或if 条件1:代码1代码2代码3else:...# 多分支if 条件1:代码1代码2代码3elif 条件2:......else:...
3.1.3 案例
age =22height=170weight=100gendel="female"print(\'我是顶级代码...\')if age > 16 and age < 26 and height >160 and gendel == "female":print("开始表白。。。。")print(\'我是顶级代码....\')
age_of_girl = input(\'请输入年龄:\')height = input(\'请输入身高:\')weight = input(\'请输入体重:\')is_pretty = input(\'请给自己颜值打分:\')if 18 <= int(age_of_girl) <= 22 and int(height) > 170 and int(weight) < 100 and int(is_pretty) >=80:print(\'Get到WeChat\')else:print(\'游泳健身了解一下?\')
score = input(\'请输入你的成绩:\')score = int(score)if score >= 90:print(\'优秀!\')elif score >= 80:print(\'良好!\')elif score >= 70:print(\'普通!\')else:print(\'很差!\')
3.1.4 if判断嵌套
age =18height=170weight=100gendel="female"is_ok=Trueprint(\'我是顶级代码...\')if 16 < age < 26 and height >160 and gendel ==\'female\':print("开始表白。。。。")if is_ok: # 如果is_ok为真则打印print("在一起。。。。")else: # 否则打印我们不合适print("我们不合适。。。")else:print("阿姨好。。。。")print(\'我是顶级代码....\')
3.2 流程判断之while循环
3.2.1 循环基础知识
-
循环就是重复做某件事
-
循环是为了让计算机像人一样去重复做某件事
-
while循环也可称为条件循环
3.2.2 while循环基础语法
while 条件:代码1代码2代码3# 基本使用count = 0while count < 6:print(count)count += 1
3.2.3 结束while循环的两种方式
- 条件改为假(必须等到下一次循环判断条件时循环才会结束)
tag = Truewhile tag:username = input("username:")password = input("password:")if username == "ccc" and password == "123":print(\'login successfully\')tag = Falseelse:print(\'username or password error\')
- break干掉本层循环,不会进行下一次循环
while tag:username = input("username:")password = input("password:")if username == "ccc" and password == "123":print(\'login successfully\')breakelse:print(\'username or password error\')
3.2.4 循环嵌套
tag = Truewhile tag:while tag:while tag:tag = False
while 条件1:while 条件2:while 条件3:breakbreakbreak
3.2.5 while+continue
- 终止本次循环,直接进入下一次
count = 0while count < 6:if count == 3:count += 1continueprint(count)count += 1# 0 1 2 4 5continue下面的代码都不会执行,因此continue下面不要写代码!!!切记!!!
3.2.6 while+else
- 情况一:while循环非正常结束,else不执行
count = 0while count < 6:if count == 3:breakprint(count)count += 1else:print(\'=\'*5)# 0 1 2
- 情况二:while正常结束,执行else
count = 0while count < 6:if count == 3:continueprint(count)count += 1else:print(\'=\'*10)# 0 1 2 4 5# ==========
3.2.7 死循环
- 表达式永远为真的循环称为死循环
count = 0while count < 4:print(count)while True:print(\'ok\')while 1:print(\'ok\')
3.2.8 案例
username = "ccc"password = "123"print(\'顶级代码....\')while True:inp_name=input("请输入用户名:")inp_pwd=input("请输入密码:")if inp_name == username and inp_pwd == password:print(\'login successful\')breakelse:print(\'username or password error\')print("====end====")
username = "ccc"password = "123"tag = Trueprint(\'顶级代码....\')while True:inp_name = input("请输入用户名:")inp_pwd = input("请输入密码:")if inp_name == username and inp_pwd == password:print(\'login successful\')tag = Falseelse:print(\'username or password error\')print("====end====")
username = "ccc"password = "123"count = 0tag = Truewhile tag:inp_user = input("请输入用户名:").strip()inp_pwd = input("请输入密码:").strip()if inp_user == username and inp_pwd == password:print("登录成功")while tag:print("""0:退出1:转账2:取款3:查询余额""")inp_order = input("请输入命令:").strip()if inp_order == "0":print("退出")tag=Falseelif inp_order == "1":print("转账")elif inp_order == "2":print("取款")elif inp_order == "3":print("查询余额")else:print("输入命令有误")tag = Falseelse:print("用户或密码错误")count += 1if count == 3:print("输入次数已超限制")tag = False
3.3 流程判断之for循环
3.3.1 for循环基础
-
对于循环取值while并不擅长,python有专门循环取值的操作:for循环
-
for循环也称为迭代循环
-
for循环与while循环的相同之处在于都是循环,都可以用来做重复的事情
-
for循环与while循环的不同之处在于while循环常用来循环执行某段代码,for循环用来循环取值
-
while循环的次数取决于条件何时为假
-
for循环的次数取决于in后数据包含的值得个数
3.3.2 for循环基本用法
- 遍历值
info = [["name", "egon"], ["age", 18], ["gender", "male"]]for x, y in info:print(x, y)# name egon# age 18# gender male
names=["egon","Lxx_dsb","xc","zhoujielun"]for i in names: # 有几个值就循环几次,4次print(i)# egon# Lxx_dsb# xc# zhoujielun
d={"k1":111,"k2":222,"k3":333}for i in d:print(i) # 从字典里取出的默认是key:k1 k2 k3print(i, d[i]) # 通过key取value:k1 111 k2 222 k3 333
3.3.3 for+range()
-
range产生一个数字序列,顾头不顾尾
range(起始位置,结束位置,步长)起始位置默认0,步长默认1
-
用途:for+range()可以按照索引编列列表
1、用来重复n次此段代码
2、range可以产生数字序列,数字对应的是列表的索引
range(10) #等同于range(0, 10)range(10, 1, -1) # 从大到小必须指定步长for i in range(5):print(i)# 0 1 2 3 4
# 在python2中>>> range(5), type(range(5))([0, 1, 2, 3, 4], <type \'list\'>)# 在python3中>>> range(5), type(range(5))(range(0, 5), <class \'range\'>)
l = [111, 222, 333, 444, 555]i = len(l) - 1while i > -1:print(l[i])i -= 1# 555 444 333 222 111或l = [111, 222, 333, 444, 555]for i in range(len(l) - 1, -1, -1):print(l[i])# 555 444 333 222 111
3.3.4 for+enumerate
- 以key:value输出
l = [111, 222, 333, 444, 555]for i in enumerate(l):print(i)# (0, 111)# (1, 222)# (2, 333)# (3, 444)# (4, 555)
l=[111,222,333,444,555]for i,y in enumerate(l):print("index: %s value: %s" % (i,y))# index: 0 value: 111# index: 1 value: 222# index: 2 value: 333# index: 3 value: 444# index: 4 value: 555
3.3.5 for+break
for i in range(3):username=input(\'username>>: \')password=input(\'password>>: \')if username == \'egon\' and password == \'123\':breakelse:print(\'用户名或密码错误\')
3.3.6 for+continue
for i in range(5): # 0 1 2 3 4if i == 2:continueprint(i) # 0 1 3 4
3.3.7 for+else
- 循环正常结束才会执行,否则不执行
for i in range(5):if i == 3:breakprint(i)else:print("---------") # 不执行
for i in range(5):if i == 3:continueprint(i)else:print("--------") # 执行
3.3.8 for循环嵌套
for i in range(3):print("--------loop1------> %s" % i)for j in range(5):print("loop2--------> %s" % j)# --------loop1------> 0# loop2--------> 0# loop2--------> 1# loop2--------> 2# loop2--------> 3# loop2--------> 4# --------loop1------> 1# loop2--------> 0# loop2--------> 1# loop2--------> 2# loop2--------> 3# loop2--------> 4# --------loop1------> 2# loop2--------> 0# loop2--------> 1# loop2--------> 2# loop2--------> 3# loop2--------> 4