分支结构 什么是分支结构 分支结构就是根据条件判断的真假去执行不同分支对应的子代码
为什么要用分支结构 人类某些时候需要根据条件来决定做什么事情,比如:’如果今天下雨,就带伞’
所以程序中必须有相应的机制来控制计算机具备的人的这种判断能力
如何使用分支结构 if语法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 if 条件1 : 代码1 代码2 ...... elif 条件2 : 代码3 代码4 ...... elif 条件3 : 代码5 代码6 ...... else : 代码7 代码8 ...... 1 python用相同缩进(4 个空格表示一个缩进)来标识一组代码块,用一组代码会自上而下依次运行 2 条件可以使任意表达式,但执行结果必须为布尔类型 2.1 None , 0 , 空(空字符串, 空列表, 空字典等)三种情况下转换成的布尔值为False 2.2 其余均为True
if应用案例 案例1:
1 2 3 4 5 如果:女人的年龄>30 岁,那么:叫阿姨 age_of_girl=31 if age_of_girl > 30 : print ('阿姨好' )
案例2:
1 2 3 4 5 6 7 如果:女人的年龄>30 岁,那么:叫阿姨,否则:叫小姐 age_of_girl=18 if age_of_girl > 30 : print ('阿姨好' ) else : print ('小姐好' )
案例3:
1 2 3 4 5 6 7 8 9 10 如果:女人的年龄>=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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 如果:成绩>=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嵌套
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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 2 3 4 5 6 7 8 练习1 : 登陆功能 name=input ('请输入用户名字:' ).strip() password=input ('请输入密码:' ).strip() if name == 'tony' and password == '123' : print ('tony login success' ) else : print ('用户名或密码错误' )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 练习2 : ''' egon --> 超级管理员 tom --> 普通管理员 jack,rain --> 业务主管 其他 --> 普通用户 ''' name=input ('请输入用户名字:' ) if name == 'egon' : print ('超级管理员' ) elif name == 'tom' : print ('普通管理员' ) elif name == 'jack' or name == 'rain' : print ('业务主管' ) else : print ('普通用户' )
循环结构 什么是循环结构? 循环结构就是重复执行某段代码块
为什么要用循环结构?
人类某些时候需要重复做某些事情 所以程序中必须有相应的机制来控制计算机具备人的这种循环做事的能力
如何使用循环结构? while循环语法 1 2 3 4 5 6 7 8 9 python中有while 和for 两种循环机制,其中while 循环称之为条件循环,语法如下 while 条件: 代码1 代码2 代码3 1 如果条件为真,那么依次执行:代码1 , 代码2 , 代码3. .. 2 执行完毕后再次判断条件,如果条件为True 则代码再次执行:代码1 , 代码2 , 代码3. .. 如果条件为False ,则循环终止
while循环应用案例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 案例一: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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 案例二: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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 案例三: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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 案例四: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
1 2 3 4 5 6 7 8 9 10 案例五:while +continue 的使用 break 代表结束本层循环,而continue 则用于结束本次循环,直接进入下一次循环number=11 while number>1 : number -= 1 if number==7 : continue print (number)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 案例五: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 2 3 4 5 6 7 8 9 练习1 : 寻找1 到100 之间数字7 最大的倍数(结果是98 ) number=100 while number>0 : if number%7 ==0 : print (number) break number-=1
1 2 3 4 5 6 7 8 9 10 11 12 练习2 : age=18 count=0 while count<3 : count+=1 guess = int (input (">>:" )) if guess > age : print ("猜的太大了,往小里试试..." ) elif guess < age : print ("猜的太小了,往大里试试..." ) else : print ("恭喜你,猜对了..." )
for循环语法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 循环结构的第二种实现方式是for 循环,for 循环可以做的事情while 循环都可以实现,之所以用for 循环,是因为在循环取值(即遍历值)时,for 循环比while 循环的使用更为简洁 for 循环语法如下 ↓↓↓ for 变量名 in 可迭代对象: 代码一 代码二 ... for item in ['a' ,'b' ,'c' ]: print (item) a b c
for循环应用案例 1 2 3 4 案例一:打印数字0 -5 for count in range (6 ): print (count)
1 2 3 4 5 count = 0 while count < 6 : print (count) count += 1
1 2 3 4 案例二:遍历字典 for k in {'name' :'jason' ,'age' :18 ,'gender' :'male' }: print (k)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 案例三:for 循环嵌套 ***** ***** ***** for i in range (3 ): for j in range (5 ): print ("*" ,end='' ) print () 注意:break 与 continue 也可以用于for 循环,使用语法同while 循环
1 2 3 4 5 6 7 8 9 练习一: 打印九九乘法表 for i in range (1 ,10 ): for j in range (1 ,i+1 ): print ('%s*%s=%s' %(i,j,i*j),end=' ' ) print ()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 练习二: 打印金字塔 ''' #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 号数=2current_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 (2current_level-1 ):print ('' ,end='' ) print ()