流程控制if、while、for

if判断

  if判断想执行第一个条件,if后的判断必须是True

1 什么是if判断
  判断一个条件如果成立则做...不成立则做....
2 为何要有if判断
  让计算机能够像人一样具有判断的能力
3 如何用if判断

语法1:

  if 条件1:
    code1
    code2
    code3
    ......

语法2:if-else

  if 条件:
    code1
    code2
    code3
    ......
  else:
    code1
    code2
    code3
    ......

语法3:嵌套

   if 条件1:
    if 条件2:
      code1
      code2
      code3
    code2
    code3
    ......

语法4:elif   

     if 条件1:
    子代码块1
   elif 条件2:
    子代码块2
   elif 条件3:
    子代码块3
   elif 条件4:
    子代码块4
    .........
   else:
    子代码块5

 代码练习

score = input('your score>>: ')
score=int(score)
if score >= 90:  #不满足的必然小于90,elif只需要>=80就行,不用判断与90的关系
    print('优秀')
elif score >= 80:
    print('良好')
elif score >= 70:
    print('普通')
else:
    print('很差')

 while循环判断

1. 什么是循环
  循环指的是一个重复做某件事的过程

2. 为何要有循环
  为了让计算机能够像人一样重复做某件事
3. 如何用循环

while循环的语法:while循环又称为条件循环,循环的次数取决于条件

     while 条件:
      子代码1
      子代码2
      子代码3

登录账号密码验证:

1  print('start....')
2  while True:
3      name=input('please your name>>: ')
4      pwd=input('please your password>>: ')
5      if name == 'egon' and pwd == '123':
6          print('login successful')
7      else:
8          print('user or password err')
9  print('end...')

结束while循环的方式:

 方式一:操作while循环的条件让其结束

  控制条件的两种方式:

判断count:
count=1
while count < 6:
     print(count)
     count+=1

判断布尔
tag=True
count=1
while tag:
     if count > 5:
         tag=False
     print(count)
     count+=1
 1  print('start....')
 2  tag=True
 3  while tag:
 4      name=input('please your name>>: ')
 5      pwd=input('please your password>>: ')
 6      if name == 'egon' and pwd == '123':
 7          print('login successful')
 8          tag=False
 9      else:
10          print('user or password err')
11 
12  print('end...')

 方式二: break强行终止本层循环

 1  print('start....')
 2  while True:
 3      name=input('please your name>>: ')
 4      pwd=input('please your password>>: ')
 5      if name == 'egon' and pwd == '123':
 6          print('login successful')
 7          break
 8      else:
 9          print('user or password err')
10 
11  print('end...')

输错三次账号或密码直接退出

 1  方式一:
 2  print('start....')
 3  count=0
 4  while count <= 2: #count=3
 5      name=input('please your name>>: ')
 6      pwd=input('please your password>>: ')
 7      if name == 'egon' and pwd == '123':
 8          print('login successful')
 9          break
10      else:
11          print('user or password err')
12          count+=1
13 
14  print('end...')
15 
16 
17  方式二:
18  print('start....')
19  count=0
20  while True:
21      if count == 3:
22          print('输错的次数过多傻叉')
23          break
24      name=input('please your name>>: ')
25      pwd=input('please your password>>: ')
26      if name == 'egon' and pwd == '123':
27          print('login successful')
28          break
29      else:
30          print('user or password err')
31          count+=1
32 
33  print('end...')
continue :结束本次循环,直接进入下一次
break :结束本层循环
count=1
 while count < 6:
     if count ==  4:
         count+=1 #如果不加一,会进入死循环
         continue # 只能在cotinue同一级别之前加代码,之后的代码不会执行
     print(count) #print 不能放在if之前,否则排除不了4
     count+=1


 while True:
     print('11111')
     print('22222')
     print('333')
     continue # 不应该将continue作为循环体最后一步执行的代码反正也会重新开始循环,相当于多此一举
while+else count=1 while count < 6: if count == 4: break print(count) count+=1 else: print('会在while循环没有被break终止的情况下执行')#如果while循环被终止了的话,else后的代码不会执行

输错三次则退出之while+else的应用代码:

 1  print('start....')
 2  count=0
 3  while count <= 2: #count=0,1,2  3的时候是第四次
 4      name=input('please your name>>: ')
 5      pwd=input('please your password>>: ')
 6      if name == 'egon' and pwd == '123':
 7          print('login successful')
 8          break
 9      else:
10          print('user or password err')
11          count+=1
12  else:
13      print('输错的次数过多')
14 
15  print('end...')
while循环的嵌套,内部循环输入4,直接退出程序,即退出两个循环

复杂版:
 1  name_of_db='egon'
 2  pwd_of_db='123'
 3  print('start....')
 4  count=0
 5  while count <= 2: #count=3
 6      name=input('please your name>>: ')
 7      pwd=input('please your password>>: ')
 8      if name == name_of_db and pwd == pwd_of_db:
 9          print('login successful')
10          while True:
11              print("""
12              1 浏览商品
13              2 添加购物车
14              3 支付
15              4 退出
16              """)
17              choice=input('请输入你的操作: ') #choice='1'
18              if choice == '1':
19                  print('开始浏览商品....')
20              elif choice == '2':
21                  print('正在添加购物车....')
22              elif choice == '3':
23                  print('正在支付....')
24              elif choice == '4':
25                  break
26          break
27      else:
28          print('user or password err')
29          count+=1
30  else:
31      print('输错的次数过多')
32 
33  print('end...')
 简洁版:定义tag变量控制所有循环
 1 name_of_db='egon'
 2 pwd_of_db='123'
 3 tag=True
 4 print('start....')
 5 count=0
 6 while tag:
 7     if count == 3:
 8         print('尝试次数过多')
 9         break
10     name=input('please your name>>: ')
11     pwd=input('please your password>>: ')
12     if name == name_of_db and pwd == pwd_of_db:
13         print('login successful')
14         while tag:
15             print("""
16             1 浏览商品
17             2 添加购物车
18             3 支付
19             4 退出
20             """)
21             choice=input('请输入你的操作: ') #choice='1'
22             if choice == '1':
23                 print('开始浏览商品....')
24             elif choice == '2':
25                 print('正在添加购物车....')
26             elif choice == '3':
27                 print('正在支付....')
28             elif choice == '4':
29                 tag=False
30 
31     else:
32         print('user or password err')
33         count+=1
34 
35 print('end...')

for循环主要用于循环取值

普通方法:

1 student=['egon','虎老师','lxxdsb','alexdsb','wupeiqisb']
3 i=0
4 while i < len(student):  #len为列表的方法,相当于Java中数组的长度方法
5      print(student[i])
6      i+=1 

用for循环取值:

 1  student=['egon','虎老师','lxxdsb','alexdsb','wupeiqisb']
 2 
 3 #打印每一个列表元素
 4  for item in student:
 5      print(item)
 6 
 7  #打印字符串的每个字符
 8  for item in 'hello':
 9      print(item)
10 
11  #只能取出key,然后通过key来去对应的值   
12  dic={'x':444,'y':333,'z':555}
13  for k in dic:
14      print(k,dic[k])
15 
16  #range:范围,1-10,取头不取尾,步数为3;
17  for i in range(1,10,3):
18      print(i)
19  >>1,4,7
20 
21  #省略起点0,默认步长为1,结果是0-9,十个数
22  for i in range(10):
23      print(i)

取出编号并对应老师名:

1 student=['egon','虎老师','lxxdsb','alexdsb','wupeiqisb']
2 for i in range(len(student)):#len(student)=5,即range(0,5),i=0,1,2,3,4
3     print(i,student[i])

运行结果:

  0 egon
  1 虎老师
  2 lxxdsb
  3 alexdsb
  4 wupeiqisb

原文地址:https://www.cnblogs.com/xuechengeng/p/9647574.html