七、流程控制

流程控制

顺序结构
   代码自上而下依次运行(我们之前所写的代码都是顺序结构)
分支结构
   代码运行到某个节点之后根据条件的不同执行不同的代码(if判断)
循环结构
   代码运行到某个节点之后一直重复执行某一段代码直到结束(while for

 

流程控制之分支结构if

"""
if 条件:
  条件成立之后才会执行的代码

1.什么是if判断

  接收用户输入的用户名
  接受用户输入的密码
  判断 输入的用户名 等于 正确的用户名 并且 输入的密码 等于 正确的密码:
      告诉用户登陆成功
  否则:
      告诉用户登陆失败

2.为何用
  让计算机能够像人一样去判断

3.如何用
if 条件1:
  子代码1
  子代码2
  子代码3
elif 条件2:
  子代码1
  子代码2
  子代码3
......
else:
  子代码1
  子代码2
  子代码3

如果条件成立则走if子代码
如果条件不成立则走else子代码

条件1成立执行if子代码 elif和else都不执行
条件2成立(说明条件1没有成立) 执行elif的子代码 if和else都不执行
如果条件都不成立 则走最后的else子代码 if和elif都不执行
elif可以写多个

必备知识
  代码缩进
  在python中使用缩进来表示代码的从属关系
  一般情况下我们采取四个空格来表示缩进(推荐)

print(123)
  print(789) 并不是所有的代码都可以拥有子代码

  并不是所有的代码都可以拥有子代码
  截至目前为止只有if可以拥有子代码

  同属于一个关键字的子代码必须要保持相同的缩进量
  ps:遇到冒号下面的代码必缩进
"""

一 单分支

# gender = 'female'
# age = 19
# is_beautiful = True
# height = 1.7
#
# if gender == 'female' and 16 < age < 20 and 1.58 < height < 1.8 and is_beautiful:
#     print('yes')
# print('end.........')

二 双分支

# gender = 'female'
# age = 19
# is_beautiful = True
# height = 1.7
#
# if gender == 'female' and 16 < age < 20 and 1.58 < height < 1.8 and is_beautiful:
#     print('yes')
# else:
#     print('no')

# 案例:
# username = input('请输入您的用户名>>>>:')
# pwd = input('请输入您的密码>>>>:')
# if username == 'mal' and pwd == '666':
#     print('bingo')
# else:
#     print('err')

三 多分支

# score = input('请输入您的成绩:')
# score = int(score)
# if score >= 90:
#     print('秀儿')
# elif score >= 80:
#     print('针不戳')
# elif score >= 70:
#     print('啧啧啧')
# else:
#     print('啥也不是')

 

流程控制之循环结构while

一 while循环语法:

"""
一 while循环语法:

while 条件:
  代码1
  代码2
  ...
当条件成立的情况下 会依次执行while子代码
子代码运行结束后再次返回条件处判断条件是否成立 如果成立继续执行
如果不成立立即结束循环
"""

# 示例1
# 打印1-5
# i = 1
# while i <= 5:
#     print(i)
#     i += 1

二 死循环:永不结束的循环

# while True:
#     print(1)

# while True:
#     name = input('>>')
#     print(name)

三 结束while循环的两种方式

# 方式一:把条件改为False,必须要等到下一次循环判断条件是才能结束循环
# i = 1
#
# tag = True
# while tag:
#     if i == 5:
#         tag = False
#     print(i)
#     i += 1

# tag = True
# while tag:
#     username = input('请输入您的用户名>>>>:')
#     pwd = input('请输入您的密码>>>>:')
#     if username == 'mal' and pwd == '666':
#         print('bingo')
#         tag = False
#     else:
#         print('err')
#     print('end..........') # 此行代码还会运行

# 方式二:break终止本层循环
# tag = True
# i = 1
# while tag:
#     if i == 5:
#         break
#     print(i)
#     i += 1


# while True:
#     username = input('请输入您的用户名>>>>:')
#     pwd = input('请输入您的密码>>>>:')
#     if username == 'mal' and pwd == '666':
#         print('bingo')
#         break
#     else:
#         print('err')


# i = 0
# while True:
#     username = input('请输入您的用户名>>>>:')
#     pwd = input('请输入您的密码>>>>:')
#     if username == 'mal' and pwd == '666':
#         print('bingo')
#         break
#     else:
#         print('err')
#     if i == 3:
#         print('尝试超过3次,结束')
#         break

四 补充

# 补充
# while True:
#     while True:
#         while True:
#             break
#         break
#     break
#
# tag = True
# while tag:
#     while tag:
#         tag = False
#     tag = False

五 while+continue:continue终止本次循环直接进入下一次

# 打印1-5,除了3
# i = 1
# while i <= 5:
#     if i == 3:
#         i += 1
#         continue
#     print(i)
#     i += 1
# 强调
# 1.不要在continue后加与continue同级代码,加了永远运行不了
# 2.循环体代码的最后一步不要写continue

六 while+else

# else的子代码何时运行:
# while循环结束后并且在正常情况下结束的,else的子代码才会运行
# ps:只要不是被break干掉的循环,都是正常结束的
# i = 0
# while i <= 10:
#     print(i)
#     i += 1
# else:
#     print('.......')

 

流程控制之循环结构for

一 for更擅长遍历值,for循环的次数取决于值的个数

 

# nums = [111, 222, 333, 444, 555]
# for i in nums:
#     print(i)

# for x in 'hello':
#     print(x)

# dic = {'k1':111,'k2':222,'k3':333}
# for k in dic:
#     print(k,dic[k])

# items = [['name','egon'],['age',18],['gender','male']]
# for x in items:
#     print(x) # ['name', 'egon'] ['age', 18] ['gender', 'male']
# for x,y in items:
#     print(x,y) # name egon age 18 gender male

二 for+break

# nums = [111, 222, 333, 444, 555]
# for i in nums:
#     if i == 333:
#         break
#     print(i)

三 for +continue

# nums = [111, 222, 333, 444, 555]
# for i in nums:
#     if i == 333:
#         continue
#     print(i)

四 for + else

# nums = [111, 222, 333, 444, 555]
# for i in nums:
#     if i == 333:
#         continue
#     print(i)
# else:
#     print('....')

# 让某段代码重复运行3次-》while循环实现如下
# i = 0
# while i < 3:
#     print('hello1')
#     print('hello2')
#     i += 1

# 让某段代码重复运行3次-》for循环实现如下
# for x in range(3):
#     print('hello1')
#     print('hello2')

六 for +enumerate

# nums = [111, 222, 333, 444, 555]
# for i,num in enumerate(nums):
#     print(i,num)

 

 
posted @ 2021-07-31 11:22  MAL嘟嘟嘟  阅读(39)  评论(0)    收藏  举报