流程控制即控制流程,具体指控制程序的执行流程,而程序的执行流程分为三种结构:顺序结构(之前我们写的代码都是顺序结构)、分支结构(用到if判断)、循环结构(用到while与for)
分支结构就是根据条件判断的真假去执行不同分支对应的子代码
人类某些时候需要根据条件来决定做什么事情,比如:如果今天下雨,就带伞
所以程序中必须有相应的机制来控制计算机具备人的这种判断能力
用if关键字来实现分支结构,完整语法如下
| if 条件1: |
| 代码1 |
| 代码2 |
| ...... |
| elif 条件2: |
| 代码3 |
| 代码4 |
| ...... |
| elif 条件3: |
| 代码5 |
| 代码6 |
| ...... |
| else: |
| 代码7 |
| 代码8 |
| ...... |
| |
| |
| |
| |
| |
| |
案例1:
如果:女人的年龄>30岁,那么:叫阿姨
| age_of_girl=31 |
| if age_of_girl > 30: |
| print('阿姨好') |
案例2:
如果:女人的年龄>30岁,那么:叫阿姨,否则:叫小姐
| age_of_girl=18 |
| if age_of_girl > 30: |
| print('阿姨好') |
| else: |
| print('小姐好') |
案例3:
如果:女人的年龄>=18并且<22岁并且身高>170并且体重<100并且是漂亮的,那么:表白,否则:叫阿姨**
| age_of_girl=18 |
| height=171 |
| weight=99 |
| is_pretty=True |
| if age_of_girl >= 18 and age_of_girl < 22 and height > 170 and weight < 100 and is_pretty == True: |
| print('表白...') |
| else: |
| print('阿姨好') |
案例4:
如果:成绩>=90,那么:优秀
如果成绩>=80且<90,那么:良好
如果成绩>=70且<80,那么:普通
其他情况:很差
| score=input('>>: ') |
| score=int(score) |
| |
| if score >= 90: |
| print('优秀') |
| elif score >= 80: |
| print('良好') |
| elif score >= 70: |
| print('普通') |
| else: |
| print('很差') |
案例5:if嵌套
| |
| |
| |
| |
| age_of_girl=18 |
| height=171 |
| weight=99 |
| is_pretty=True |
| success=False |
| |
| if age_of_girl >= 18 and age_of_girl < 22 and height > 170 and weight < 100 and is_pretty == True: |
| if success: |
| print('表白成功,在一起') |
| else: |
| print('什么爱情不爱情的,爱nmlgb的爱情,爱nmlg啊...') |
| else: |
| print('阿姨好') |
练习1: 登陆功能
| name=input('请输入用户名字:').strip() |
| password=input('请输入密码:').strip() |
| if name == 'tony' and password == '123': |
| print('tony login success') |
| else: |
| print('用户名或密码错误') |
练习2:
| |
| |
| |
| ''' |
| egon --> 超级管理员 |
| tom --> 普通管理员 |
| jack,rain --> 业务主管 |
| 其他 --> 普通用户 |
| ''' |
| name=input('请输入用户名字:') |
| |
| if name == 'egon': |
| print('超级管理员') |
| elif name == 'tom': |
| print('普通管理员') |
| elif name == 'jack' or name == 'rain': |
| print('业务主管') |
| else: |
| print('普通用户') |
循环结构就是重复执行某段代码块
人类某些时候需要重复做某件事情
所以程序中必须有相应的机制来控制计算机具备人的这种循环做事的能力
python中有while与for两种循环机制,其中while循环称之为条件循环,语法如下
| while 条件: |
| 代码1 |
| 代码2 |
| 代码3 |
| ...... |
| |
| |
| |
插图:while循环
案例一:while循环的基本使用
用户认证程序
| |
| username = "jason" |
| password = "123" |
| |
| inp_name = input("请输入用户名:") |
| inp_pwd = input("请输入密码:") |
| if inp_name == username and inp_pwd == password: |
| print("登陆成功") |
| else: |
| print("输入的用户名或密码错误!") |
| |
| username = "jason" |
| password = "123" |
| |
| |
| inp_name = input("请输入用户名:") |
| inp_pwd = input("请输入密码:") |
| if inp_name == username and inp_pwd == password: |
| print("登陆成功") |
| else: |
| print("输入的用户名或密码错误!") |
| |
| |
| inp_name = input("请输入用户名:") |
| inp_pwd = input("请输入密码:") |
| if inp_name == username and inp_pwd == password: |
| print("登陆成功") |
| else: |
| print("输入的用户名或密码错误!") |
| |
| |
| inp_name = input("请输入用户名:") |
| inp_pwd = input("请输入密码:") |
| if inp_name == username and inp_pwd == password: |
| print("登陆成功") |
| else: |
| print("输入的用户名或密码错误!") |
| |
| |
| |
| |
| username = "jason" |
| password = "123" |
| |
| count = 0 |
| while count < 3: |
| inp_name = input("请输入用户名:") |
| inp_pwd = input("请输入密码:") |
| if inp_name == username and inp_pwd == password: |
| print("登陆成功") |
| else: |
| print("输入的用户名或密码错误!") |
| count += 1 |
案例二:while+break的使用
使用了while循环后,代码确实精简多了,但问题是用户输入正确的用户名密码以后无法结束循环,那如何结束掉一个循环呢?这就需要用到break了!
| username = "jason" |
| password = "123" |
| |
| count = 0 |
| while count < 3: |
| inp_name = input("请输入用户名:") |
| inp_pwd = input("请输入密码:") |
| if inp_name == username and inp_pwd == password: |
| print("登陆成功") |
| break |
| else: |
| print("输入的用户名或密码错误!") |
| count += 1 |
案例三:while循环嵌套+break
如果while循环嵌套了很多层,要想退出每一层循环则需要在每一层循环都有一个break
| username = "jason" |
| password = "123" |
| count = 0 |
| while count < 3: |
| inp_name = input("请输入用户名:") |
| inp_pwd = input("请输入密码:") |
| if inp_name == username and inp_pwd == password: |
| print("登陆成功") |
| while True: |
| cmd = input('>>: ') |
| if cmd == 'quit': |
| break |
| print('run <%s>' % cmd) |
| break |
| else: |
| print("输入的用户名或密码错误!") |
| count += 1 |
案例四:while循环嵌套+tag的使用
针对嵌套多层的while循环,如果我们的目的很明确就是要在某一层直接退出所有层的循环,其实有一个窍门,就让所有while循环的条件都用同一个变量,该变量的初始值为True,一旦在某一层将该变量的值改成False,则所有层的循环都结束
| username = "jason" |
| password = "123" |
| count = 0 |
| |
| tag = True |
| while tag: |
| inp_name = input("请输入用户名:") |
| inp_pwd = input("请输入密码:") |
| if inp_name == username and inp_pwd == password: |
| print("登陆成功") |
| while tag: |
| cmd = input('>>: ') |
| if cmd == 'quit': |
| tag = False |
| break |
| print('run <%s>' % cmd) |
| break |
| else: |
| print("输入的用户名或密码错误!") |
| count += 1 |
案例五:while+continue的使用
break代表结束本层循环,而continue则用于结束本次循环,直接进入下一次循环
| |
| number=11 |
| while number>1: |
| number -= 1 |
| if number==7: |
| continue |
| print(number) |
案例五:while+else的使用
在while循环的后面,我们可以跟else语句,当while 循环正常执行完并且中间没有被break 中止的话,就会执行else后面的语句,所以我们可以用else来验证,循环是否正常结束
| count = 0 |
| while count <= 5 : |
| count += 1 |
| print("Loop",count) |
| else: |
| print("循环正常执行完啦") |
| print("-----out of while loop ------") |
| 输出 |
| Loop 1 |
| Loop 2 |
| Loop 3 |
| Loop 4 |
| Loop 5 |
| Loop 6 |
| 循环正常执行完啦 |
| -----out of while loop ------ |
如果执行过程中被break,就不会执行else的语句
| count = 0 |
| while count <= 5 : |
| count += 1 |
| if count == 3: |
| break |
| print("Loop",count) |
| else: |
| print("循环正常执行完啦") |
| print("-----out of while loop ------") |
| 输出 |
| Loop 1 |
| Loop 2 |
| -----out of while loop ------ |
练习1:
寻找1到100之间数字7最大的倍数(结果是98)
| number=100 |
| while number>0: |
| if number%7==0: |
| print(number) |
| break |
| number-=1 |
练习2:
| age=18 |
| count=0 |
| while count<3: |
| count+=1 |
| guess = int(input(">>:")) |
| if guess > age : |
| print("猜的太大了,往小里试试...") |
| elif guess < age : |
| print("猜的太小了,往大里试试...") |
| else: |
| print("恭喜你,猜对了...") |
循环结构的第二种实现方式是for循环,for循环可以做的事情while循环都可以实现,之所以用for循环是因为在循环取值(即遍历值)时for循环比while循环的使用更为简洁,
for循环语法如下
| for 变量名 in 可迭代对象: |
| 代码一 |
| 代码二 |
| ... |
| |
| |
| for item in ['a','b','c']: |
| print(item) |
| |
| a |
| b |
| c |
| |
| |
| |
| |
| |
案例一:打印数字0-5
| |
| for count in range(6): |
| print(count) |
| |
| |
| count = 0 |
| while count < 6: |
| print(count) |
| count += 1 |
案例二:遍历字典
| |
| for k in {'name':'jason','age':18,'gender':'male'}: |
| print(k) |
| |
| |
案例三:for循环嵌套
| |
| ***** |
| ***** |
| ***** |
| |
| for i in range(3): |
| for j in range(5): |
| print("*",end='') |
| print() |
注意:break 与 continue也可以用于for循环,使用语法同while循环
练习一:
打印九九乘法表
| for i in range(1,10): |
| for j in range(1,i+1): |
| print('%s*%s=%s' %(i,j,i*j),end=' ') |
| print() |
练习二:
打印金字塔
| |
| ''' |
| #max_level=5 |
| * # current_level=1,空格数=4,*号数=1 |
| *** # current_level=2,空格数=3,*号数=3 |
| ***** # current_level=3,空格数=2,*号数=5 |
| ******* # current_level=4,空格数=1,*号数=7 |
| ********* # current_level=5,空格数=0,*号数=9 |
| |
| # 数学表达式 |
| 空格数=max_level-current_level |
| *号数=2*current_level-1 |
| ''' |
| |
| max_level=5 |
| for current_level in range(1,max_level+1): |
| for i in range(max_level-current_level): |
| print(' ',end='') |
| for j in range(2*current_level-1): |
| print('*',end='') |
| print() |